Laravel crud

In this topic, we will learn how to create the laravel crud in laravel 5.8.

The following are the steps required to build the crud app:

  • First, we create a project named as 'crud' in laravel 5.8.
Laravel crud

Laravel crud

The above screenshot shows that the 'crud' project has been created successfully.

  • Now, we create the database in phpMyAdmin.
Laravel crud

In the above screenshot, we have provided the database name as laravel_crud.

  • Our application will work with a laravel_crud database. Edit the .env file.
Laravel crud

The above screenshot shows that we have modified the .env file. We have provided database name as laravel_crud to the DB_Database field, root to the DB_Username. We left blank to the password field.

  • Now, we create the migration to create a table in a laravel_crud database shown in the below screenshot:
Laravel crud

The above-highlighted statement creates a migration "create_user_table", and the name of the table is 'user'.

  • Open the migration file that you created in the above step.
  1.      <?php  
  2.      use Illuminate\Support\Facades\Schema;  
  3.      use Illuminate\Database\Schema\Blueprint;  
  4.      use Illuminate\Database\Migrations\Migration;  
  5.      class CreateUserTable extends Migration  
  6. {  
  7. /** 
  8. * Run the migrations. 
  9. * 
  10. * @return void 
  11.  */  
  12.  public function up()  
  13. {  
  14.       Schema::create('user'function (Blueprint $table) {  
  15.       $table->bigIncrements('id');  
  16.       $table->string('first_name');  
  17.             $table->string('last_name');  
  18.             $table->string('gender');  
  19.             $table->string('qualifications');  
  20.             $table->timestamps();  
  21.         });  
  22.     }  
  23.   
  24.     /** 
  25.      * Reverse the migrations. 
  26.      * 
  27.      * @return void 
  28.      */  
  29.     public function down()  
  30.     {  
  31.         Schema::dropIfExists('user');  
  32.     }  
  33. }  

We have created four new columns (first name, last name, gender, qualifications) in the user table shown in the above code.

  • To migrate the above changes in a laravel_crud database, we use the command given below:

php artisan migrate;

Laravel crud

After migration, look at the database given in the below screenshot:

Laravel crud

The above screen shows that the user table has been created under the laravel_crud database.

  • Now, we need to create a model to perform a database operations.

The above screen shows that the 'Crud' model has been created successfully.

  • After the creation of a model, we will move to the app folder where the crud model is created.

Crud.php

  1. <?php  
  2. namespace App;  
  3. use Illuminate\Database\Eloquent\Model;  
  4. class Crud extends Model  
  5. {  
  6.     //  
  7. protected $table='user';  
  8. protected $fillable=['first_name','last_name','gender','qualifications'];  
  9. }  

In the above model, we have provided two attributes, i.e., $table and $fillable. The $table is an attribute that contains the name of the table, which Crud model is going to use while the $fillable attribute contains the array of the names of the columns which cannot be NULL.

  • Now, we create a controller with a resource that implements all the CRUD operations.
Laravel crud

The above screenshot shows that the CrudsController has been created successfully.

The structure of CrudsController is given below:

  1. <?php  
  2. namespace App\Http\Controllers;  
  3. use Illuminate\Http\Request;  
  4. class CrudsController extends Controller  
  5. {  
  6.     /** 
  7.      * Display a listing of the resource. 
  8.      * 
  9.      * @return \Illuminate\Http\Response 
  10.      */  
  11.     public function index()  
  12.     {  
  13.         //  
  14.     }  
  15.   
  16.     /** 
  17.      * Show the form for creating a new resource. 
  18.      * 
  19.      * @return \Illuminate\Http\Response 
  20.      */  
  21.     public function create()  
  22.     {  
  23.         //  
  24.     }  
  25.   
  26.     /** 
  27.      * Store a newly created resource in storage. 
  28.      * 
  29.      * @param  \Illuminate\Http\Request  $request 
  30.      * @return \Illuminate\Http\Response 
  31.      */  
  32.     public function store(Request $request)  
  33.     {  
  34.         //  
  35.     }  
  36.   
  37.     /** 
  38.      * Display the specified resource. 
  39.      * 
  40.      * @param  int  $id 
  41.      * @return \Illuminate\Http\Response 
  42.      */  
  43.     public function show($id)  
  44.     {  
  45.         //  
  46.     }  
  47.   
  48.     /** 
  49.      * Show the form for editing the specified resource. 
  50.      * 
  51.      * @param  int  $id 
  52.      * @return \Illuminate\Http\Response 
  53.      */  
  54.     public function edit($id)  
  55.     {  
  56.         //  
  57.     }  
  58.   
  59.     /** 
  60.      * Update the specified resource in storage. 
  61.      * 
  62.      * @param  \Illuminate\Http\Request  $request 
  63.      * @param  int  $id 
  64.      * @return \Illuminate\Http\Response 
  65.      */  
  66.     public function update(Request $request$id)  
  67.     {  
  68.         //  
  69.     }  
  70.   
  71.     /** 
  72.      * Remove the specified resource from storage. 
  73.      * 
  74.      * @param  int  $id 
  75.      * @return \Illuminate\Http\Response 
  76.      */  
  77.     public function destroy($id)  
  78.     {  
  79.         //  
  80.     }  
  81. }  

The CrudsController contains the inbuilt functions (index(), create(), store(), show(), edit(), update(), destroy()).

Now, we implement the CRUD operations through the methods available in the CrudsController.

Insert Operation

  • First, we create the route in the web.php file that performs the insert operation.
  1. Route::get('/insert'function () {  
  2.     return view('create');  
  3. });  
  • Now, we create a view named as create.blade.php in resources/views directory.
  1. @extends('layout.master')  
  2. @section('content')  
  3. <form method="post" action="{{ route('users.store') }}">  
  4.    @csrf     
  5.           <div class="form-group">      
  6.               <label for="first_name">First Name:</label><br/><br/>  
  7.               <input type="text" class="form-control" name="first_name"/><br/><br/>  
  8.           </div>  
  9. <div class="form-group">      
  10. <label for="first_name">Last Name:</label><br/><br/>  
  11.               <input type="text" class="form-control" name="last_name"/><br/><br/>  
  12.           </div>  
  13. <div class="form-group">      
  14.               <label for="gender">Gender:</label><br/><br/>  
  15.               <input type="text" class="form-control" name="gender"/><br/><br/>  
  16.           </div>  
  17. <div class="form-group">      
  18.               <label for="qualifications">Qualifications:</label><br/><br/>  
  19.               <input type="text" class="form-control" name="qualifications"/><br/><br/>  
  20.           </div>  
  21. <br/>  
  22. <button type="submit" class="btn-btn" >Insert</button>  
  23. </form>  
  24. @endsection  

Output of the above code would be:

Laravel crud
  • The above code calls the store function of the CrudsController class, and the code of the store() function is given below:
  1. public function store(Request $request)  
  2.    
  3.     {  
  4.     
  5.         $request->validate([  
  6.             'first_name'=>'required',  
  7.             'last_name'=>'required',  
  8.             'gender'=>'required',  
  9.             'qualifications'=>'required'  
  10.         ]);  
  11.   
  12.         $crud = new Crud;  
  13.         $crud->first_name =  $request->get('first_name');  
  14.         $crud->last_name = $request->get('last_name');  
  15.         $crud->qualifications = $request->get('qualifications');  
  16.         $crud->gender = $request->get('gender');  
  17.         $crud->save();  
  18.         }  

Suppose we enter some data in the form, and then click on the Insert button shown in the below screenshot:

Laravel crud
  • Let's look at the database:
Laravel crud

The above screenshot shows that the data we entered in the form has been successfully saved in the database.

Retrieving the records

  • First, we create a route in the web.php file.

Route::get('/show','CrudsController@index');

The above statement creates a route with a url '/show' which calls the index() method of the CrudsController class.

  • The above route calls the index function of the CrudsController, and the code of the index() method is given below:
  1. public function index()  
  2.     {  
  3.         $cruds = Crud::all();  
  4.   
  5.         return view('index', compact('cruds'));  
  6.     }  

In the above code, we use the all() method that retrieves all the records of a table associated with a Crud model, and stores it in the $cruds object. We pass the $cruds object to the index.blade.php file by using the view() method.

  • The code of the index.blade.php file is given below:
  1. @extends('layout.master')  
  2. @section('content')  
  3. <table border="1px">  
  4. <thead>  
  5. <tr>  
  6. <td>  
  7. ID </td>  
  8. <td>  
  9. First Name </td>  
  10. <td>  
  11. Last Name </td>  
  12. <td>  
  13. Gender </td>  
  14. <td>  
  15. Qualifications </td>  
  16. </tr>  
  17. </thead>  
  18. <tbody>  
  19. @foreach($cruds as $crud)  
  20.         <tr border="none">  
  21.             <td>{{$crud->id}}</td>  
  22.             <td>{{$crud->first_name}}</td>  
  23.             <td>{{$crud->last_name}}</td>  
  24.             <td>{{$crud->gender}}</td>  
  25.             <td>{{$crud->qualifications}}</td>  
  26. <td >  
  27. <form action="{{ route('users.destroy', $crud->id)}}" method="post">  
  28.                   @csrf  
  29.                   @method('DELETE')  
  30.                   <button class="btn btn-danger" type="submit">Delete</button>  
  31.                 </form>  
  32. </td>  
  33. <td >  
  34. <form action="{{ route('users.edit', $crud->id)}}" method="GET">  
  35.                   @csrf  
  36.                    
  37.                   <button class="btn btn-danger" type="submit">Edit</button>  
  38.                 </form>  
  39. </td>  
  40.   
  41.          </tr>  
  42. @endforeach  
  43. </tbody>  
  44. </table>  
  45. @endsection  

Output of the above code would be:

Laravel crud

Update operation

When we click on the Edit button, then it calls the edit() function of the CrudsController class. The code of the edit() method is given below:

CrudsController.php

  1. public function edit($id)  
  2.     {  
  3.         //  
  4.      $crud= Crud::find($id);  
  5.      return view('edit', compact('crud'));  
  6.     }  

In the above code, we use find() method that finds the record of a given id, and stores it in the $crud object. We pass the crud object to the edit.blade.php file.

edit.blade.php

  1. @extends('layout.master')  
  2. @section('content')  
  3. <form method="Post" action="{{route('users.update',$crud->id)}}">  
  4. @method('PATCH')     
  5.  @csrf     
  6.           <div class="form-group">      
  7.               <label for="first_name">First Name:</label><br/><br/>  
  8.               <input type="text" class="form-control" name="first_name" value={{$crud->first_name}}><br/><br/>  
  9.           </div>  
  10.   
  11. <div class="form-group">      
  12.               <label for="first_name">Last Name:</label><br/><br/>  
  13.               <input type="text" class="form-control" name="last_name" value={{$crud->last_name}}><br/><br/>  
  14.           </div>  
  15. <div class="form-group">      
  16.               <label for="gender">Gender:</label><br/><br/>  
  17.               <input type="text" class="form-control" name="gender" value={{$crud->gender}}><br/><br/>  
  18.           </div>  
  19. <div class="form-group">      
  20.               <label for="qualifications">Qualifications:</label><br/><br/>  
  21.               <input type="text" class="form-control" name="qualifications" value={{$crud->qualifications}}><br/><br/>  
  22.           </div>  
  23. <br/>  
  24.   
  25. <button type="submit" class="btn-btn" >Update</button>  
  26. </form>  
  27.   
  28.   
  29. @endsection  

After clicking on the Edit button, the screen appears which is shown below, and it asks you to update the data.

Laravel crud

In the above screen, you can change any field according to your requirement. Suppose I entered 'Harshita' in first name, tripathi in last name, and other fields remain same, click on the Update button as shown below:

Laravel crud

After clicking on the Update button, the control moves to the update() function of the CrudsController.php file.

CrudsController.php

  1. public function edit($id)  
  2.     {  
  3.         //  
  4.      $crud= Crud::find($id);  
  5.      return view('edit', compact('crud'));  
  6.     }  

In the above code, we use find() method that finds the record of a given id, and stores it in the $crud object. We pass the crud object to the edit.blade.php file.

edit.blade.php

  1. public function update(Request $request$id)  
  2.     {  
  3.         //  
  4.  $request->validate([  
  5.             'first_name'=>'required',  
  6.             'last_name'=>'required',  
  7.             'gender'=>'required',  
  8.             'qualifications'=>'required'  
  9.         ]);  
  10.   
  11.         $crud = Crud::find();  
  12.         $crud->first_name =  $request->get('first_name');  
  13.         $crud->last_name = $request->get('last_name');  
  14.         $crud->qualifications = $request->get('qualifications');  
  15.         $crud->gender = $request->get('gender');  
  16.         
  17.         $crud->save();  
  18.     }  

The above code updates the database.

Let's look at the database:

Laravel crud

The above screenshot shows that the data has been updated successfully.

Delete operation

If we click on the delete button, then it calls the destroy() function of the CrudsController class. The code of the destroy() method is given below:

  1. public function destroy($id)  
  2.     {  
  3.         $crud=Crud::find($id);  
  4.         $crud->delete();  
  5.     }