CRUD(Create Read Update Delete) operation is important part of any web application. Every web application has CRUD system.
In this article, we will share how you can implement CRUD opearation in Laravel application. We will go through step by step. So let's get started.
Step 1: Create Laravel application
In the first step, we will create new Laravel application. So open your Terminal OR CMD and run below command:
composer create-project --prefer-dist laravel/laravel users
Now change Terminal directory to application root folder.
cd users
Step 2: Setup database and migration
In the root of application, change database variables according to your MySQL credentials.
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=users
DB_USERNAME=root
DB_PASSWORD=root
We will create CRUD opearation for users. The migration file already exists in Laravel application, so run the migrate command to create tables in database.
php artisan migrate
Step 3: Create route
In this step, we create routes in routes/web.php
file. Add the below routes in it.
<?php
use App\Http\Controllers\UserController;
Route::resource('users', UserController::class);
Step 4: Create controller class
Resource controller are the controllers which includes all the methods to handle CRUD opearation. In this step, we will create resource controller class.
php artisan make:controller UserController --resource
Now, we have need to open app/Http/Controllers/UserController.php and put the below code.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user = User::latest()->get();
return view('user.index', compact('user'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('user.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
]);
User::create($request->all());
return redirect()->route('users.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = User::where("id", $id)->first();
return view('user.show', compact('user'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::where("id", $id)->first();
return view('user.edit',compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required',
'email' => 'required',
]);
$user = User::where("id", $id)->first();
$user->update($request->all());
return redirect()->route('users.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect()->route('users.index');
}
}
Step 5: Create blade files
Now we need to create blade files to view HTML code. View files are located ar resource/views folder.
First of create app.blade.php
file in layouts
folder. This is main layout file which will extends in all view files.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
</head>
<body>
<div id="app">
<nav class="navbar navbar-expand-md navbar-light navbar-laravel">
<div class="container-fluid">
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name') }}
</a>
</div>
</nav>
<main class="py-4">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
@yield('content')
</div>
</div>
</div>
</div>
</main>
</div>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#dataTables').DataTable();
});
</script>
@yield('script')
</body>
</html>
In the resource/views
folder, create user folder and create the below files with HTML code.
index.blade.php
file to view all users data.
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel CRUD Opeartion</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('users.create') }}"> Create New user</a>
</div>
</div>
</div>
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Email</th>
<th width="280px">Action</th>
</tr>
@foreach ($user as $key=>$value)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
<td>
<a href="{{ route('users.edit',$value->id) }}" class="btn btn-primary">Edit</a>
<a href="{{ route('users.show',$value->id) }}" class="btn btn-primary">Show</a>
<a href="#" class="btn btn-danger" onclick="document.getElementById('user-{{ $value->id }}').submit();">Delete</a>
<form action="{{ route('users.destroy', $value->id)}}" method="post" id="user-{{ $value->id }}">
@csrf
@method('DELETE')
</form>
</td>
</tr>
@endforeach
</table>
@endsection
After that create create.blade.php
file to view create form.
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New user</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a>
</div>
</div>
</div>
<form action="{{ route('users.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>email:</strong>
<input type="text" name="email" class="form-control" placeholder="EMail">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Password:</strong>
<input type="password" name="password" class="form-control" placeholder="Password">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
@endsection
show.blade.php
file to show users data.
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show user</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $user->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>emails:</strong>
{{ $user->email }}
</div>
</div>
</div>
@endsection
And edit.blade.php
file to create update form.
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit user</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a>
</div>
</div>
</div>
<form action="{{ route('users.update', $user->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group">
<label class="label">Name:</label>
<input type="text" name="name" value="{{ $user->name }}" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<label class="label">email:</label>
<input type="text" name="email" value="{{ $user->email }}" class="form-control" placeholder="email">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</form>
@endsection
Now everything is ready. Run the Laravel server using artisan command.
php artisan serve
And in your browser go through below URL.
I hope you liked the article.