Laravel CRUD Tutorial Using Resource Controller

This laravel 5.6 tutorial help to create CRUD functionality using Resource Controller.CRUD aka Creating, Reading, Updating, and Deleting resources from database.You can create these operation using Laravel Resource very easily. The Laravel makes easy to create CRUD operation using Resources Controller.

Laravel very fast and popular PHP MVC framework. Laravel resource routing assigns the typical “CRUD” routes to a controller with a single line of code.

In this laravel tutorial, we will let you know the process of CRUD operation for employee module.The employee module will have create, read, update, and delete (CRUD) a resource.

CRUD Operation Using Resource Controller in Laravel 5.6

I am assuming, you have created laravel application and connected database using .env file.We will create new controller, model and view to access CRUD features.

Laravel Migration File

We will create laravel migration file using below command.
php artisan make:migration create_employee_table

Above command will create migration file into database/migrations/2018_09_17_060245_create_employee_table.php.

Now open database/migrations/2018_09_17_060245_create_employee_table.php file and add below table column information into this file.

We will migrate table into database using below command.
php artisan migrate

Above command will create employee table into your connected database.Please make sure your database settings are correct into app/config/database.php file:

We will create Employee model using below command.You can get model file into controllers/ folder
php artisan make:model Employee

You can also check other recommended tutorials of Lumen/Laravel,

Laravel has in-built command to do specific operation like create controller, create migration file and migrate table, create model, create event and listeners etc, that is grouped under Artisan command, I will use Artisan command to create resource controller using below command. This Controller will have all CRUD operation method declaration that handles all operation for employee module.

php artisan make:controller EmployeeController --resource

The above command will create EmployeeController.php file into app/Http/Controllers/ folder.The controller file will contain a method for each of the available resource operations.

php artisan make:controller EmployeeController --resource --model=Employee

Now, we will make route entry into routes/web.php file.

Route::resource('employee', 'EmployeeController');

The above resource routes will handle following operations:

HTTP Method Path (URL) Action (Method) Route Name
GET /employee index employee.index
GET /employee/create create employee.create
POST /employee store
GET /employee/{id} show
GET /employee/{id}/edit edit employee.edit
PUT/PATCH /employee/{id} update employee.update
DELETE /employee/{id} destroy employee.destroy

How To Create HTML Views in Laravel 5.6

The resources folder have all views of the application modules, You can create manually or using artisan CLI.

The following artisan CLI command will create employee module view files for templating into laravel:
php artisan make:view employee --resource

The above command has been created employee/ folder into resources/views/ folder.This folder contains index.blade.html for listing, create.blade.html for add record and edit.blade.html for update record file.

How To Create Listing in Laravel 5.6

The index blade template is use to display list all records.The controller method index() is responsible to get all record from employee table.We will add below code into resources/views/employee/index.blade.html file:

How to Add Record Into Laravel 5.6

We have created add new employee record action into EmployeeController file, Now we will create HTML file and added action method into form.

There are two controller method will participate into add new record functionality in laravel, The create() method is use to show create view and store() method is used to save the form data into database.I am using MySQL, So that record will saved into MySQL employee table.
You can add all data related operations and validation into store() method.

How to Update Record Into Laravel 5.6

We have created update employee record action into EmployeeController file, Now we will create edit HTML file using blade template engine.

The form action is set 'employee.update', The update() method is defined into EmployeeController to update data.The edit() action is defined to display edit template.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.