This laravel 9 tutorial help to create CRUD operation using laravel 9. CRUD stands for Creating, Reading, Updating, and Deleting resources from the database. I’ll create a controller using the 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 the employee module as an example to implement CRUD operation in Laravel 9. We will create an employee, read an employee record, update a data, and delete a record.
Laravel has an in-built command to do a specific operation like create controller, create migration file and migrate table, create model, create event and listeners etc, that is grouped under the Artisan command
CRUD Operation Using Resource Controller In Laravel 9
I am using MySQL DB for the employee table and created a connection. I am not using a migration file to handle MySQL table operation, just use SQL to create employee table and insert data.
MySQL Database Connection in Laravel 9
Let’s create a MySQL database connection using .env
file. We will create 'test'
database and employee table.
-- -- Database: `test` -- -- -------------------------------------------------------- -- -- Table structure for table `employee` -- CREATE TABLE `employee` ( `id` int(11) NOT NULL COMMENT 'primary key', `employee_name` varchar(100) NOT NULL COMMENT 'employee name', `employee_salary` int(11) NOT NULL COMMENT 'employee salary', `employee_age` int(11) NOT NULL COMMENT 'employee age', `profile_image` varchar(50) NOT NULL COMMENT 'profile image' ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table'; -- -- Dumping data for table `employee` -- INSERT INTO `employee` (`id`, `employee_name`, `employee_salary`, `employee_age`, `profile_image`) VALUES (1, 'Adam', 12322, 23, ''); -- -- Indexes for dumped tables -- -- -- Indexes for table `employee` -- ALTER TABLE `employee` ADD PRIMARY KEY (`id`);
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.
'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''),
Let’s open .env
file and passed below data :
DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
How To Create Model Class in Laravel 9
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.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Employee extends Model { // }
Create CRUD Controller in Laravel 9
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 9
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 9
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 9
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:
<h1>All the Employees</h1> @foreach($employees as $key => $emp) @endforeach <table class="table table-striped table-bordered"> <thead> <tr> <td>ID</td> <td>Name</td> <td>Salaray</td> <td>Age</td> <td>Actions</td> </tr> </thead> <tbody> <tr> <td>{{ $emp->id }}</td> <td>{{ $emp->name }}</td> <td>{{ $emp->salary }}</td> <td>{{ $emp->age }}</td> <!-- we will also add show, edit, and delete buttons --> <td> <!-- delete the nerd (uses the destroy method DESTROY /nerds/{id} --> <!-- we will add this later since its a little more complicated than the other two buttons --> <!-- show the nerd (uses the show method found at GET /nerds/{id} --> <a class="btn btn-small btn-success" href="{{ URL::to('employee/' . $emp->id) }}">Show</a> <!-- edit this nerd (uses the edit method found at GET /nerds/{id}/edit --> <a class="btn btn-small btn-info" href="{{ URL::to('employee/' . $emp->id . '/edit')}}">Edit</a> </td> </tr> </tbody> </table>
How To Add Record Into Laravel 9
We have created add new employee record action into EmployeeController
file, Now we will create HTML file and added the action method into the form.
<h1>Add Employee Record</h1> <!-- will be used to show any messages --> @if (Session::has('message')) <div class="alert alert-info">{{ Session::get('message') }}</div> @endif @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form method="post" action="{{ route('employee.store')}}" accept-charset="UTF-8"> <div class="col-md-12"> <div class="form-area"> <div class="alert alert-success hide"></div> <input name="_token" type="hidden" value="{{ csrf_token() }}"> <div class="alert alert-danger hide"></div> <div class="form-group"> <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}" placeholder="Name"> @if ($errors->has('name')) <span class="alert-danger">dsssssssssssssssssss</span> @endif </div> <div class="form-group"> <input type="number" pattern="[0-9]*" class="form-control" id="salary" name="salary" placeholder="Salary Number"> </div> <div class="form-group"> <input class="form-control" type="number" id="age" name="age" placeholder="age"> </div> <button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Submit Employee</button> </div> </div> </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 9
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
@if (Session::has('message')) <div class="alert alert-info">{{ Session::get('message') }}</div> @endif @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div class="col-md-12"> <div class="form-area"> <form role="form" method="post" action="{{ route('employee.update', $employee->id)}}" accept-charset="UTF-8"> <div class="alert alert-success hide"></div> <input name="_token" type="hidden" value="{{ csrf_token() }}"> <div class="alert alert-danger hide"></div> <div class="form-group"> <input type="text" class="form-control" id="name" name="name" value="{{$employee->name}}" placeholder="Name"> </div> <div class="form-group"> <input type="number" pattern="[0-9]*" class="form-control" id="salary" name="salary" value="{{ $employee->salary }}" placeholder="Salary Number"> </div> <div class="form-group"> <input class="form-control" type="number" id="age" name="age" placeholder="age" value="{{ $employee->age }}"> </div> <button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Submit Employee</button> </form> </div> </div>
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.