Laravel 5.4 Crud Example From Scratch

Welcome, Web Artisans,  In today’s AppDividend Tutorial, I have shown the code of Laravel 5.4 Crud Example From Scratch.

Laravel

For creating Laravel Project, you need to have following things.

  1. PHP >= 5.6.4
  2. OpenSSL PHP Extension
  3. PDO PHP Extension
  4. Mbstring PHP Extension
  5. Tokenizer PHP Extension
  6. XML PHP Extension
  7. Composer

Step 1: Create a Laravel project

Type following command in your terminal to create a project.

composer create-project --prefer-dist laravel/laravel Laravel54

It will take 2 minutes to install a brand new Laravel Framework

After installing, go to your project root folder and type php artisan serve in terminal, your project URL might look like this

http://localhost:8000

Now open that laravel project folder in your favorite editor.

Step 2: Edit .env file for database configuration

In your project root folder, there is a file called .env, which we need to edit to set up a database configuration. I am using MySQL database.

// .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel54
DB_USERNAME=root
DB_PASSWORD=mysql

Step 3: Use migrations provided by laravel to create users and password_resets table

Now go to terminal, and run following command

php artisan migrate

If you have created the database in MySQL and you have provided correct credentials to the .env file, then you will see there are three tables generated in MySQL database.

Step 4: Create a controller file for our CRUD operations.

Go to terminal, type the following command in your root project.

php artisan make:controller CRUDController --resource

Now go to laravel54 > app > Http > Controllers, you will see CRUDController file in that folder.

This file has all the boilerplate needed for our Crud operations.

Step 5: Create a model file for CRUD operations.

In the terminal, type the following command in your root project.

php artisan make:model Crud -m

This command will also create a migration file for our CRUD operations.

Step 6: Edit crud migration file and create the required fields for the database.

Migration file is located in laravel54 > database > migrations folder.

<?php
// create_cruds_table

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCrudsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cruds', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('post');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('cruds');
    }
}




Run the following command in your terminal.

php artisan migrate

Step 7: Create views for set up a form

Go to laravel54 > resources > views . Locate into that folder and then create a master view called master.blade.php. A blade is templating engine used by laravel.

<!-- master.blade.php -->

<!doctype html>
<html lang="{{ config('app.locale') }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>CRUD Operations</title>

        <!-- Fonts -->
        <link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css">
    </head>
    <body>
        <br><br>
        @yield('content')
    </body>
</html>

Now create a new folder inside views directory called crud. Go inside that folder and create following files

  1. index.blade.php
  2. create.blade.php
  3. edit.blade.php

Step 8: Create a form in create.blade.php

Read More
1 of 4
<!-- create.blade.php -->

@extends('master')
@section('content')
<div class="container">
  <form>
    <div class="form-group row">
      <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Title</label>
      <div class="col-sm-10">
        <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title">
      </div>
    </div>
    <div class="form-group row">
      <label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Post</label>
      <div class="col-sm-10">
        <textarea name="post" rows="8" cols="80"></textarea>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2"></div>
      <input type="submit" class="btn btn-primary">
    </div>
  </form>
</div>
@endsection

Step 9: Setup a route for the request handling.

Go to the routes > web.php

<?php

Route::get('/', function () {
    return view('welcome');
});
Route::resource('crud', 'CRUDController');

Here we have added resource route, and all the functions reside in app > Http > controllers > CRUDController

Step 10: Edit CRUDController File

// CRUDController.php

  /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('crud.create');
    }

Here we have to return the view when the request hits the route; it will be redirected to this controller’s create method. Our view should be accessible via following URL the form.

http://localhost:8000/crud/create

We are now able to see the form with the two fields.

Step 11: Add CSRF token and set the post route of the form.

<!-- create.blade.php -->

@extends('master')
@section('content')
<div class="container">
  <form method="post" action="{{url('crud')}}">
    <div class="form-group row">
      {{csrf_field()}}
      <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Title</label>
      <div class="col-sm-10">
        <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title">
      </div>
    </div>
    <div class="form-group row">
      <label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Post</label>
      <div class="col-sm-10">
        <textarea name="post" rows="8" cols="80"></textarea>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2"></div>
      <input type="submit" class="btn btn-primary">
    </div>
  </form>
</div>
@endsection

We have put the {{csrf_field()}} in the form so that malicious attack can not harm our web application.

Step 12: Code the store function and use Crud model to insert the data

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Crud;

class CRUDController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('crud.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $crud = new Crud([
          'title' => $request->get('title'),
          'post' => $request->get('post')
        ]);

        $crud->save();
        return redirect('/crud');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Here we need to create a protected field called $fillable in the Crud model. Otherwise, it will throw a mass assignment exception.

// Crud.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Crud extends Model
{
    protected $fillable = ['title','post'];
}

Now, when you fill the title and post field in our form and submit the form, the new entry will be added to the database. We can confirm it by following steps.

Step 13: Code index() function in the CRUDController File.

//CrudController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Crud;

class CRUDController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cruds = Crud::all()->toArray();
        
        return view('crud.index', compact('cruds'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('crud.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $crud = new Crud([
          'title' => $request->get('title'),
          'post' => $request->get('post')
        ]);

        $crud->save();
        return redirect('/crud');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Step 14: Need to update index.blade.php

<!-- index.blade.php -->
@extends('master')
@section('content')
  <div class="container">
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Post</th>
      </tr>
    </thead>
    <tbody>
      @foreach($cruds as $post)
      <tr>
        <td>{{$post['id']}}</td>
        <td>{{$post['title']}}</td>
        <td>{{$post['post']}}</td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
@endsection

Next, shoot the following URL.

http://localhost:8000/crud

You will see a table which has the id, title and post column with their respective data.

Step 15: Add Edit and Delete Button in the index.blade.php

<!-- index.blade.php -->

@extends('master')
@section('content')
  <div class="container">
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Post</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>
      @foreach($cruds as $post)
      <tr>
        <td>{{$post['id']}}</td>
        <td>{{$post['title']}}</td>
        <td>{{$post['post']}}</td>
        <td><a href="{{action('CRUDController@edit', $post['id'])}}" class="btn btn-warning">Edit</a></td>
        <td><a href="{{action('CRUDController@destroy', $post['id'])}}" class="btn btn-danger">Delete</a></td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
@endsection




Step 16: Create an edit function to pass the data to the edit view.

<?php

// CRUDController.php  
/**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $crud = Crud::find($id);
        
        return view('crud.edit', compact('crud','id'));

    }

Step 17: Create an edit view.

<!-- edit.blade.php -->

@extends('master')
@section('content')
<div class="container">
  <form method="post" action="{{action('CRUDController@update', $id)}}">
    <div class="form-group row">
      {{csrf_field()}}
       <input name="_method" type="hidden" value="PATCH">
      <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Title</label>
      <div class="col-sm-10">
        <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title" value="{{$crud->title}}">
      </div>
    </div>
    <div class="form-group row">
      <label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Post</label>
      <div class="col-sm-10">
        <textarea name="post" rows="8" cols="80">{{$crud->post}}</textarea>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2"></div>
      <button type="submit" class="btn btn-primary">Update</button>
    </div>
  </form>
</div>
@endsection

Here we have used one more hidden field called _method, which will be a PATCH request to the server so that we can update the data very quickly.

Step 18: Code update() in the CRUDController.

<?php

// CRUDController.php

 /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $crud = Crud::find($id);
        $crud->title = $request->get('title');
        $crud->post = $request->get('post');
        $crud->save();
        return redirect('/crud');
    }

After filling the update form, we can see the index page that our data is updated. So now only Delete functionality is remaining.

Step 19: Create a delete form to delete the data.

<!-- index.blade.php -->

@extends('master')
@section('content')
  <div class="container">
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Post</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>
      @foreach($cruds as $post)
      <tr>
        <td>{{$post['id']}}</td>
        <td>{{$post['title']}}</td>
        <td>{{$post['post']}}</td>
        <td><a href="{{action('CRUDController@edit', $post['id'])}}" class="btn btn-warning">Edit</a></td>
        <td>
          <form action="{{action('CRUDController@destroy', $post['id'])}}" method="post">
            {{csrf_field()}}
            <input name="_method" type="hidden" value="DELETE">
            <button class="btn btn-danger" type="submit">Delete</button>
          </form>
        </td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
@endsection

Step 20: Code the destroy() method in the CRUDController.

<?php
// CRUDController.php

/**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
      $crud = Crud::find($id);
      $crud->delete();

      return redirect('/crud');
    }

Here in delete functionality, we have not put the any confirm box, but it is okay for the demo. I just want you to take this demo and see how the laravel’ flow is working.

Output

Laravel 5.4 Crud Example From Scratch
Laravel 5.4 Crud Example From Scratch

There is some routes list in resource controller. Type in the terminal: php artisan route:list

$ php artisan route:list
+--------+-----------+------------------+--------------+---------------------------------------------+--------------+
| Domain | Method    | URI              | Name         | Action                                      | Middleware   |
+--------+-----------+------------------+--------------+---------------------------------------------+--------------+
|        | GET|HEAD  | /                |              | Closure                                     | web          |
|        | GET|HEAD  | api/user         |              | Closure                                     | api,auth:api |
|        | GET|HEAD  | crud             | crud.index   | App\Http\Controllers\CRUDController@index   | web          |
|        | POST      | crud             | crud.store   | App\Http\Controllers\CRUDController@store   | web          |
|        | GET|HEAD  | crud/create      | crud.create  | App\Http\Controllers\CRUDController@create  | web          |
|        | GET|HEAD  | crud/{crud}      | crud.show    | App\Http\Controllers\CRUDController@show    | web          |
|        | PUT|PATCH | crud/{crud}      | crud.update  | App\Http\Controllers\CRUDController@update  | web          |
|        | DELETE    | crud/{crud}      | crud.destroy | App\Http\Controllers\CRUDController@destroy | web          |
|        | GET|HEAD  | crud/{crud}/edit | crud.edit    | App\Http\Controllers\CRUDController@edit    | web          |

If you are curious how resource controller works behind the scenes, then this is your answer.

Github: https://github.com/KrunalLathiya/Laravel54

 

Steps to use Github code

  1. Clone the repo in your local.
  2. Go to root of the project and run command “composer update
  3. Edit .env file and use your MySQL database credentials.
  4. Run the command “php artisan migrate
  5. Now, we need to bootstrap Laravel server so run “php artisan serve
  6. If now go to this URL: http://localhost:8000/crud/create

If you still have doubt in this Laravel 5.4 Crud Example From Scratch, then ask in the comment below, I am happy to help you out.

You might also like More from author

34 Comments

  1. prem says

    hello sir i am new in laravel 5.4. I downloaded your project from github and when running your project gives an error “(1/1) NotFoundHttpException
    in RouteCollection.php (line 179)” . i din’t modified any code just setup database connection.

    1. Krunal says

      Have you done composer install?

  2. Mauro Martin says

    Thans a lot for this tutorial! I follow all of the steps but using diferent data ( a books table with ISBN title and Author fields) and I got all results OK You´re MASTER. Thanks again
    Mauro Martin from Colombia

    1. Krunal says

      Glad to hear that my article helps you. Thank you

  3. Ady says

    Hello Krunal I have same like this bt i am not getting the the view first of it was like blank page even i have added the source as you have mentioned after that it is displaying object not found.

    I want to thank you for making these for beginners like me it is easy to use..

  4. Ryan says

    is it best practice to write query in controller instead of model in laravel?

    1. Krunal says

      Actually, we can go with both approaches. IF only CRUD operations are there then we definitely go with controller file but if there is more logic behind any of the modules then we can go with models. Also, some folks create services file and in that they write queries. So whatever approach suits to your application but make sure that if you are working in a team then all follow the same convention and flow.

  5. md ali says

    sir, i have getting the error “Warning: require(C:\xampp\htdocs\Lvl54CRUD\bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Lvl54CRUD\bootstrap\autoload.php on line 17

    Fatal error: require(): Failed opening required ‘C:\xampp\htdocs\Lvl54CRUD\bootstrap/../vendor/autoload.php’ (include_path=’.;C:\xampp\php\PEAR’) in C:\xampp\htdocs\Lvl54CRUD\bootstrap\autoload.php on line 17”
    when i run your CRUD code…
    pls reply…

    1. Krunal says

      Please first check your php version which is above 5.6 and then fire command composer install. I guess this will resolve your issue.

  6. md ali says

    i have already installed composer…
    then wt i can do to solve this….

  7. Krunal says

    Please, use composer install command and then go to routes >> web.php file check the routes in that file and according to that fire the URL

  8. Shariq says

    After step 14 , i get this error

    (1/1) FatalErrorException
    Class ‘App\Http\Controllers\Crud’ not found
    in CRUDController.php (line 16)

    1. Krunal says

      in that CRUDController file
      put

      use App\Crud

      It does not find any model that’s why

  9. Fakhrul says

    sir I have problem with the database
    when I create title and post then the information doesnt enter the database
    how i can solve this problem?
    Thanks for your help

    1. Krunal says

      Check the model was included properly or fill the protected $fillable field with your column fields.

  10. Frida says

    After step 14, i get the error ErrorException
    Undefined variable: cruds (View: ..\resources\views\crud\index.blade.php)

    1. Krunal says

      have you wrote this function in CRUDController,

      /**
      * Display a listing of the resource.
      *
      * @return \Illuminate\Http\Response
      */
      public function index()
      {
      $cruds = Crud::all()->toArray();

      return view(‘crud.index’, compact(‘cruds’));
      }

  11. Narendra Vyas says

    Hi,
    After 18 th step I am getting error “MethodNotAllowedHttpException”, even it’s not coming inside update function.

    After google I came to know that this error might occur due to method type as in this update case it is PUT|PATCH. So what may be the cause of this issue. Any help

    Rest works fine for me.

    Thanks.

  12. Dhiraj Sonavane says

    Excellent article.

  13. Kin says

    Im Brazillian, congrats dude! Ill use to base for my all projects from now.

  14. macky says

    Hi. Thanks a lot for this one. This helps me a lot especially CRUD. Anyways my strength framework is codeigniter. Your code seems preety straigthforward and easy to understand.

  15. Ariane says

    Hi! I have been struggling for a while now. Please is there a way to create only an api with laravel without involving any form or anything? just an api that will interact with a mobile app. Thank you!

    1. Krunal says

      Yes, you if you are using Laravel 5.4 then you can use api.php file in routes folder. Define all the routes specific to your mobile application needs and then call the http request like api/ products or api/product/create. You need to remember that you have to always put a prefix api for every routes in api.php file.

  16. Riva says

    I will right away take hold of your rss feed as I can not to find your e-mail subscription link or newsletter service. Do you have any? Kindly allow me realize so that I may subscribe. Thanks.

  17. B says

    Nice guide. I am getting this error midway through it though

    (1/1) FatalErrorException
    Class ‘App\Http\Controllers\Crud’ not found
    in CRUDController.php (line 16)

    1. Krunal says

      Include CRUD model in your controller like use App\CRUD;

      1. B says

        Thank you!

  18. Jerry says

    Thank you!
    Great post!

Leave A Reply

Your email address will not be published.