Simple Laravel CRUD Operation Using Laravel 6 — Phpflow.com

This laravel 6 tutorial help to create CURD operation using laravel 6.CRUD stands for Creating, Reading, Updating, and Deleting resources from database.I ll create controller using Laravel Resource command.
Laravel very fast and popular PHP MVC framework.The Laravel resource routing assigns the typical “CRUD” routes to a controller with a single line of code.
I will take employee module as an example to implement CRUD operation in Laravel 6.We will create an employee, read an employee record, update a data, and delete a record.
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
CRUD Operation Using Resource Controller In Laravel 6
I am using MySQL db for employee table an created connection.I am not using migration file to handle MySQL table operation, just use SQL to create employee table and insert data.
MySQL Database Connection in Laravel 6
Let’s create MySQL database connection using .env
file. We will create 'test'
database and employee table.
Created 'employee'
table and inserted one record into this table.The employee table has id column which is a primary key.
Please make sure your database settings are correct into app/config/database.php
file.There is following entries are important for MySQL connection.
Let’s open .env
file and passed below data :
How To Create Model Class in Laravel 6
We will create Employee model using below command.You can get model file into app/
folderphp artisan make:model Employee
Above command will create a Employee.php
file the app/
folder.
Create CRUD Controller in Laravel 6
I will use Artisan command to create resource controller using below command.
php artisan make:controller EmployeeController --resource
The above command will create EmployeeController.php
file into app/Http/Controllers/
folder.The controller file contains all CRUD operation method declaration that handles all operation for employee module.
Create Route Entry Into Laravel 6
The web.php
file is responsible for routes into the web application.We ll make following entry into this file.
Route::resource('employee', 'EmployeeController');
How To Create HTML Views In Laravel 5.6
The resources folder have all views of the application, You can create views files 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 6
We have already created resources/views/employee/index.blade.html
file. The index blade template is use to display all records.The controller method index()
is responsible to get all record from employee table.We will add below code into index.blade.html
file:
How To Add Record Into Laravel 6
We have created add new employee record action into EmployeeController
file, Now we will create HTML file and added action method into form.
You can see two method into the controller file, The create()
method is use to show create view and store()
method is used to save the form data into database.The store()
method also use to do data related operations and validation.
How To Update Record Into Laravel 6
We have created update employee record action into EmployeeController.php
file, Now we will create edit HTML file using blade template engine.Added below code into the edit.blade.html
file
The EmployeeController
has two method, one is edit()
- that is responsible to display edit template and update()
method is used to save data.
The form action is set 'employee.update'
to call update()
method.
Originally published at https://www.phpflow.com on April 15, 2020.