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.
Let’s perform create, read, update, and delete operations on a database using Laravel 10.
There are the following pre-requisite for this tutorial:
We’ll follow the following steps to create the CRUD Laravel app:
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
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(); }); }
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
Let’s create the employee table in the database by running the below command:
php artisan make:model Employee
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 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 new directory named `emps`
inside the `resources/views`
directory and add the following files:
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.
php artisan serve
Open your web browser and navigate to `http://localhost:8000/emps`
to see the list of employees.
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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More