in this Laravel tutorial, We’ll demonstrate how to perform CRUD operations with Laravel and MySQL. The CRUD(creates, reads, updates, and deletes) operations on a database using Laravel 10 with the help of Eloquent. Laravel application using Laravel’s Eloquent ORM and perform the operation.
CRUD Functionality in Laravel
Let’s perform create, read, update, and delete operations on a database using Laravel 10.
There are the following pre-requisite for this tutorial:
- XAMPP
- Composer
We’ll follow the following steps to create the CRUD Laravel app:
- Install Laravel and create a new application
- Create a database
- Create a table using migartion
- Create a controller
- Set up the model
- Add a route
- Generate Blade files
- run laravel application
Create a New Laravel Application
Open your terminal to create your Laravel application by running the following command:
composer create-project laravel/laravel laravel-crud
Once Your project has been created, You’ll have to install the Laravel dependencies. To do so, execute the following command inside your project directory:
composer update
Create a Database and Table
Let’s create a ‘test’ database and create a new migration file using the below command:
php artisan make:migration create_employees_table --create=employees
You can get more information from Laravel migartion.
public function up() { Schema::create('employees', function (Blueprint $table) { $table->id(); $table->string('name'); $table->integer('salary'); $table->integer('age'); $table->timestamps(); }); }
Connect Your database with ‘Test’ db:
You need to edit your application’s .env
file at the root of your Laravel application. Locate the variables prefixed with DB_
and edit them with your database credentials:
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3036 DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=pass
It would help if you replaced variable values as per your MySQL host credentials.
Run the migrations files:
php artisan migrate
Create A Model
Let’s create the employee table in the database by running the below command:
php artisan make:model Employee
Create a Controller Using Model
Let’s create a controller file scaffolding of all CRUD methods using Artisan:
php artisan make:controller EmployeeController --resource --model=Employee
The above command creates an EmployeeController.php
file in app/Http/Controllers
, This file contains the empty function index, store, show, update, and destroy.
The updated Code:
use App\Models\Employee use Illuminate\Http\Request; class EmployeeController extends Controller { public function index() { $emps = Employee::all(); return view('emps.index', ['emps' => $emps]); } public function create() { return view('emps.create'); } public function store(Request $request) { $validatedData = $request->validate([ 'name' => 'required|string|max:255', 'age' => 'required', 'salary' => 'required', ]); Employee::create($validatedData); return redirect() ->route('emps.index') ->with('success', 'Employee created successfully.'); } public function show(Employee $emp) { return view('emps.show', ['emp' => $emp]); } public function edit(string $id) { $emp = Employee::findOrFail($id); return view('emps.edit', ['emp' => $emp]); } public function update(Request $request, string $id) { $emp = Employee::findOrFail($id); $validatedData = $request->validate([ 'name' => 'required|string|max:255', 'age' => 'required', 'salary' => 'required', ]); $emp->update($validatedData); return redirect() ->route('emps.index') ->with('success', 'Employee updated successfully.'); } public function destroy(string $id) { $emp = Employee::findOrFail($id); $emp->delete(); return redirect()->route('emps.index')->with('success', 'Employee deleted successfully.'); } }
Define Routes
Define the routes for users and emps in the `routes/web.php`
file:
// routes/web.php use App\Http\Controllers\EmployeeController; Route::resource('emps', EmployeeController::class);
Create a Views blade template
Create a new directory named `emps`
inside the `resources/views`
directory and add the following files:
- resources/index.blade.php
- resources/create.blade.php
- resources/show.blade.php
- resources/edit.blade.php
I am creating one index.blade.php file to display all listings of employee data.
<! - resources/views/emps/index.blade.php --> <!DOCTYPE html> <html> <head> <title>Employee List</title> </head> <body class="bg-gray-200"> <a href="{{url('/')}}"> <button class="bg-sky-500 my-2 mx-2 text-white text-black px-3 py-2 h-10" type="submit">Home</button> </a> <div class="flex justify-center items-center content-center flex-col align-items-center min-h-screen border-2 border-black"> <div class="w-1/4"> <div class="flex justify-between w-full"> <h1 class="text-xl font-bold">Employee List</h1> <a class="ms-3 bg-sky-500 text-white text-black px-3 py-2" href="{{ route('emps.create') }}">Create</a> </div> <ul> @foreach ($emps as $emp) <li class="my-3 list-none bg-white px-10 py-3"> <div class="flex justify-between items-center"> <div class="flex flex-col "> <p> {{ ucwords($emp->name) }} - .{{ $emp->age }} - .{{ $emp->salary }} </p> </div> <div class="flex items-center"> <a class="ms-3 bg-sky-500 text-white text-black px-3 py-2 h-10" href="{{ route('emps.edit', $emp->id) }}">Edit</a> <form method="post" class="pt-4s" action="{{ route('emps.destroy', $emp->id) }}"> @csrf @method('DELETE') <button type="submit" class="ms-3 bg-red-500 text-white text-black place-self-center px-3 py-2">Delete</button> </form> </div> </div> </li> @endforeach </ul> </div> </div> </body> </html>
You need to create the rest of the views file.
Run the development server:
php artisan serve
Open your web browser and navigate to `http://localhost:8000/emps`
to see the list of employees.
Conclusion
The popular Laravel framework is ideal for developing web applications. The CRUD functionality is necessary for scalable web applications. This article explained the basic ideas behind CRUD operations and how to use Laravel’s built-in functionality to implement them.