How to create a CRUD Application using Laravel 8

Gethmi Pathirana
6 min readNov 13, 2020
Laravel 8 CRUD using Eloquent Model

Laravel is a PHP based web framework. Laravel uses a MVC approach, which is also known as the model-view-controller approach. In this article, I am going to guide you on how to create a basic CRUD application for beginners using Laravel 8. First off, we need to install some applications which we will use in the implementation. I have provided the links down below so you can download them.

Command: Laravel new Student

So.. let us get started.

First Step: Set up Laravel and create a project

First you need to create a project. To do that open your command prompt(cmd) , go to your preferred location and type,

Laravel new (projectName)

In my case I created a new project named Student.

Now that you created a new Laravel project on your system, you are good to go.

Second Step: Set up the Database

Here in this article I am using MySQL. Open the XAMPP control panel and start running Apache and MySQL. To create a new database click the Admin button next to the Start/Stop button of MySQL. Then it will automatically open up a new browser window with phpMyAdmin interface.

XAMPP Control Panel

Create a new database and make sure to add changes to the .env file on your code editor.

Add the name of the database you’ve created along with the DB_USERNAME

Third Step: Create migration

Here we are going to implement those CRUD operations in our newly created project.First, you have to change the directory of your cmd to the folder that contains your project(cd crudapp). Now we are ready to migrate the data.

php artisan make:migration create_projects_table — create=projects

Migration was successful.

Code-editor →database →migration →to the latest migration

Then you can add the attributes according to your preference with the relevant data type. Here I am going to add attributes like name(String), course(String), address(String) and mobile number(Integer).

Add the attributes here under the recent migration (app->database->migration->recent one)

Before we run the migration we need to specify the default string length first.For that go to the,

App →Providers →AppServiceProvider.php

And change the default string length and the root as shown below,

Change the default string length to 191
AppServiceProvider.php

Then you can run the migration command

Php artisan migrate

After the migration we need to add routes to perform our CRUD operations. Laravel provides us with these routes which will take care of these create, retrieve, update and delete operations. Go to the,

Routes →Web.php

Then add the resource route, also use the

web.php

Fourth Step: Add controller and model

In the previous step we used a model called StudentController, so we need to create the controller and specify the model by adding a model option. Resource model as the Student.

php artisan make:controller StudentController

php artisan make:model Student

Or else you can manually create the controller and model as well. Go to the

App → HTTP → Controller

Right click and create a new Controller.

DevController and Project model

Now a controller and model for our project has been created under

App →HTTP →Controller and App →Models.

Student.php(model)

Under the controller file that you created, you can see several methods like,

  1. Index()
  2. create()
  3. store(Request $request)
  4. show($id)
  5. edit($id)
  6. update(Request $request, $id)
  7. destroy($id)

I wrote the code for the different methods,

StudentController.php

<?phpnamespace App\Http\Controllers;use App\Models\User;use App\Models\Student;use Illuminate\Http\Request;use App\Http\Controllers\Controller;class StudentController extends Controller{public function index(){$students = Student::all();return view('studentform',compact('students',$students));//return view('addstudent');}public function create(){return view('addstudent');}public function store(Request $request){$request->validate(['name' => 'required','course' => 'required','address' => 'required','mobile' => 'required']);$students = new Student;$students->name=$request->input('name');$students->course=$request->input('course');$students->address=$request->input('address');$students->mobile=$request->input('mobile');$students->save();return redirect('/students')->with('success', 'Data added successfully');}public function show($id){}public function edit($id){$students =Student::find($id);return view('studenteditdata',compact('students','id'));}public function update(Request $request, $id){$request->validate(['name' => 'required','course' => 'required','address' => 'required','mobile' => 'required']);$students=Student::find($id);$students->name=$request->input('name');$students->course=$request->input('course');$students->address=$request->input('address');$students->mobile=$request->input('mobile');$students->save();return redirect('/students')->with('success', 'Data updated successfully');}public function destroy($id){$students =Student::find($id);$students->delete();return redirect('/students')->with('success', 'Data Deleted successfully');}}

To add new students I used a form. Here are the blade files that I used to create and edit students for this project.

studentform.blade.php

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"></head><body><div class="container"><div class="row"><div class="col-lg-12 margin-tb"><div class="float-left"><h2>Student Personal Data </h2></div><div class="float-right"><a class="badge badge-dark" href="/add-data"  title="Create a new student"     >Add</a></div><!--         <div  class="pull-right"><a href="/add-data" class="btn btn-outline-primary"> Add Student </a></div> --></div></div><br>@if(count($errors) >0)<div class="alert alert-danger"><ul>@foreach($errors->all() as $error)<li>{{$error}}</li>@endforeach</ul></div>@endif@if(\Session::has('success'))<div class="alert alert-success"><p>{{\Session::get('success') }}      </p></div>@endif<table class="table table-striped table-secondary"><thead><tr><th scope="col">Id</th><th scope="col">Name</th><th scope="col">Course</th><th scope="col">Address</th><th scope="col">Mobile No</th><th scope="col">Action</th><th scope="col">Delete</th></tr></thead><tbody>@foreach($students as $student)<tr><th>{{$student->id}}</th><th>{{$student->name}}</th><th>{{$student->course}}</th><th>{{$student->address}}</th><th>{{$student->mobile}}</th><th><!-- edit data --><a href="{{action('StudentController@edit',$student['id'])}}" class="btn btn-outline-success"> Edit </a></th><th><form action="{{action('StudentController@destroy',$student['id'])}}" method="post">{{csrf_field() }}<input type="hidden" name="_method" value="DELETE" ><button type="submit" name="submit" class="btn btn-outline-danger">Delete</button></form></th></tr>@endforeach</tbody></table></div></div></body></html>

studenteditdata.blade.php

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"></head><body><div class="container"><div class="jumbotrong"><div class="col-md-6"><h2> Update  Data </h2><form action="{{action('StudentController@update',$id)}}" method="post">{{csrf_field() }}{{ method_field('PUT') }}<div class="form-group"><label>Name:</label><input type="text" name="name" id="name"  value="{{ $students->name}}" class="form-control" placeholder="Enter name"></div><div class="form-group"><label>Course:</label><input type="text" name="course"  id="course"  value="{{ $students->course}}" class="form-control" placeholder="Enter course"></div><div class="form-group"><label>Address:</label><input type="text" name="address"  id="address"  value="{{ $students->address}}" class="form-control" placeholder="Enter address"></div><div class="form-group"><label>Mobile No:</label><input type="text" name="mobile" id="mobile"  value="{{ $students->mobile}}" class="form-control" placeholder="Enter mobile"></div><button type="submit" name="submit" class="btn btn-outline-warning">Update</button></form></div></div></div></body></html>

So now that everything is done, now you can run the project.

php artisan serve

Once you run “php artisan serve” you’ll get this message

Then all you need to do is, copy and paste the URL in a browser along with the route name(/route_name) that you have written under the web.php file using the resource controller.

http://127.0.0.1:8000/add-data

Form validation
Edit data

And Voila! You have made your Laravel CRUD application. I hope you found this article useful. Happy coding and remember to hit that👏🏽

--

--