This article provides a comprehensive, step-by-step guide to building a Laravel CRUD (Create, read, update, delete) application, specifically for managing employee data. Here is a recap of the steps involved:
1. Create a Laravel Project
-
Install Laravel using the following command:
composer create-project --prefer-dist laravel/laravel laravel-crud
2. Setup MySQL Database
-
Create a database (e.g.,
Laravel-crud
) in PHPMyAdmin or through a MySQL client. -
Configure the database connection in
.env
file:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel-crud DB_USERNAME=root DB_PASSWORD=
3. Create Migration for Employees Table
-
Create the migration file:
php artisan make:migration create_employees_table --create=employees
-
Define the schema in the migration:
public function up() { Schema::create('employees', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email'); $table->integer('age'); $table->integer('salary'); $table->timestamps(); }); }
-
Run the migration:
php artisan migrate
4. Create Model
-
Create a model for the employees' table:
php artisan make:model Employee
-
Define the
$fillable
property in theEmployee
model:protected $fillable = ['name', 'email', 'age', 'salary'];
5. Create Controller and Routes
-
Create a resource controller for employees:
php artisan make:controller EmployeeController --resource
-
Register routes for the
EmployeeController
inroutes/web.php
:Route::resource('employees', 'EmployeeController');
6. Configure Bootstrap
-
Install
laravel/ui
package for Bootstrap scaffolding:composer require laravel/ui php artisan ui bootstrap npm install npm run dev
7. Create the Layout
-
Create a
layout.blade.php
file inresources/views/
for common layout:<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Laravel 8 CRUD</title> <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> @yield('content') </div> <script src="{{ asset('js/app.js') }}" type="text/js"></script> </body> </html>
8. Implement the Create Operation
-
Create the
create.blade.php
form for adding employees. -
In the
EmployeeController
, add thecreate()
method to return the form:public function create() { return view('employees.create'); }
-
In
store()
method, save the employee data:public function store(Request $request) { $employeeData = $request->validate([ 'name' => 'required', 'email' => 'required', 'age' => 'required', 'salary' => 'required' ]); Employee::create($employeeData); return redirect('employees')->with('success', 'Employee created successfully.'); }
9. Implement the Read Operation
-
In
index()
method, retrieve and display all employees:public function index() { $employees = Employee::all(); return view('employees.index', compact('employees')); }
-
Display the employee data in
index.blade.php
with action buttons to edit and delete.
10. Implement the Update Operation
-
Create
edit.blade.php
for editing employee details. -
In
edit()
method, load the employee details:public function edit($id) { $employee = Employee::findOrFail($id); return view('employees.edit', compact('employee')); }
-
In
update()
method, update employee details:public function update(Request $request, $id) { $employeeData = $request->validate([ 'name' => 'required', 'email' => 'required', 'age' => 'required', 'salary' => 'required' ]); $employee = Employee::findOrFail($id); $employee->update($employeeData); return redirect('employees')->with("success", "Employee details updated successfully."); }
11. Implement the Delete Operation
-
In
destroy()
method, delete the employee:public function destroy($id) { $employee = Employee::findOrFail($id); $employee->delete(); return redirect('employees')->with("success", "Employee deleted successfully."); }
12. EmployeeController.php
-
The full
EmployeeController
contains all the methods for CRUD operations (index, create, store, edit, update, destroy).
Now, run the Laravel development server:
php artisan serve
Visit http://127.0.0.1:8000/employees
to see the CRUD operations in action.
This covers all the necessary steps to build a complete CRUD application in Laravel!