Crud Operation In Laravel Using Ajax
In this tutorial, we will learn how to do crud operations in Laravel with Ajax.
So, we will create an employee management app where we can add, update, and delete employees.
This task will help you for your interview scenario because many companies give tasks that add users or employees in Laravel using Ajax.
STEP 1: Create a Laravel app
laravel new laravel-ajax-crud-operationSTEP 2: Go to the project directory
cd laravel-ajax-crud-operationSTEP 3: Create an Employee model and migration
Now create a model and migration for employees where you store your employee data like first name, last name email phone, and password or other fields which you want. use the below artisan command to create a model and migration together.
php artisan make:model Employee -mSTEP4: Fill migration file
now go to the database/migrations/employees migration file and write the schema fields that you want in your employee table. In this migration file, we write some basic fields like first name, last name, email, phone, and password.
STEP 5: Now start your xampp server and run the migration
now run the php artisan migrate command to create database tables. If your database is not created then it will create a database that is present in your env file and then migrate your all migrations in the database. So let's start migrations and see below screenshots of migrations and the database.
php artisan migrate



STEP 6: Create Employee Controller.
Now your database is ready and it is time to create controller and blade files for further steps.
Create EmployeeController and write code for employees' add, edit, update, and delete.
php artisan make:controller EmployeeControllerSTEP 7: Write code in EmployeeCOntroller
STEP 8: White front-end code in the welcome.blade.php file
Go to resources/views/welcome.blade.php file and write the below code for setup front-end
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="csrf-token" content="{{ csrf_token() }}"><title>Laravel Ajax CRUDtitle><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js">script><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css"/><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js">script><style>input[type=text],[type=email],[type=number],[type=password]{box-shadow: none !important;border-top:0;border-left:0;border-right:0;}style>head><body><div class="container mt-5"><div class="row justify-content-center"><div class="col-md-8"><div class="card"><div class="card-header"><a class="btn btn-primary mb-2" data-bs-toggle="modal" data-bs-target="#addEmployeeModal">Add Employeea>@if(session("success_msg"))<span class="text-success">{{session("success_msg")}}span>@endif@if(session("error_msg"))<span class="text-danger">{{session("error_msg")}}span>@endifdiv><div class="card-body"><div>@if($employees->count()>0)<table class="table table-striped"><thead><tr><th>First Nameth><th>Last Nameth><th>Emailth><th>Phoneth><th>Actionth>tr>thead><tbody>@foreach ($employees as $employee)<tr id="table_row_{{$employee->id}}"><td>{{$employee->firstname}}td><td>{{$employee->lastname}}td><td>{{$employee->email}}td><td>{{$employee->phone}}td><td><a href="#" id="editEmployee" data-id={{$employee->id}} data-bs-toggle="modal" data-bs-target="#addEmployeeModal"><i class="fa fa-pen">i>a><a href="#" id="deleteEmployee" data-id={{$employee->id}} class="ms-3"><i class="fa fa-trash">i>a>td>tr>@endforeachtbody>table>@endifdiv>div>div>div>div>div><div class="modal fade" id="addEmployeeModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"><div class="modal-dialog modal-dialog-centered"><div class="modal-content"><div class="modal-header"><h1 class="modal-title fs-5">Register Employee<div class="text-danger print-error-msg fs-6" style="display: none"><ul>ul>div>h1><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" onclick="clearData()">button>div><div class="modal-body"><form onsubmit="addUpdate(event)" id="addUpdateForm" autocomplete="off"><div class="row g-3"><div class="col-md-6"><div class="form-floating"><input type="text" name="firstname" class="form-control" id="firstname" placeholder="firstname@example.com" required><label for="firstname">First Namelabel>div>div><div class="col-md-6"><div class="form-floating"><input type="text" name="lastname" class="form-control" id="lastname" placeholder="lastname@example.com" required><label for="lastname">Last Namelabel>div>div><div class="col-md-12"><div class="form-floating mb-2"><input type="email" name="email" class="form-control" id="email" placeholder="email@example.com" required><label for="email">Emaillabel>div>div><div class="col-md-12"><div class="form-floating mb-2"><input type="number" name="phone" class="form-control" id="phone" placeholder="phone@example.com" required><label for="phone">Phonelabel>div>div><div class="col-md-6"><div class="form-floating mb-2"><input type="password" name="password" class="form-control" id="password" placeholder="pasword@example.com" required><label for="password">Passwordlabel>div>div>div><input type="hidden" name="employeeId" id="employeeId"><button class="btn btn-primary" type="submit">Submitbutton>form>div>div>div>div><script src="{{asset('assets/js/employees.js')}}">script>body>html>
STEP 9: Now Create an employee.js file in the public directory and write Ajax code
Now create one employee.js file in the public directory. we create this file in the public/assets/js/employees.js directory. In this file, you can write your Ajax code. If you want to write Ajax code in the same blade file then you can write it in the same file. but for easy understanding, I suggest you write it in a separate file.

STEP 10: Set routes in the web.php file
Now set routes in the web.php file.
<?phpuse Illuminate\Support\Facades\Route;use App\Http\Controllers\{EmployeeController};Route::get('/', [EmployeeController::class,'index']);Route::post('add-update-employee',[EmployeeController::class,'addUpdateEmployee']);Route::get('edit-employee/{employeeId}',[EmployeeController::class,'editEmployee']);Route::delete('delete-employee/{employeeId}',[EmployeeController::class,'deleteEmployee']);
STEP 11: Now run your project and test it.
php artisan serveOUTPUT




