Using Resource Controller we can do easily CRUD operation in Laravel Framework.
Steps for Laravel CRUD with Resource Controller
- Install Laravel Project
- Database Connection
- Creating Model, Controller, Migrations
- Creating Resource Route
- Controller and Blade Template Setup
- Then execute the CRUD
First install the laravel with run this command
composer create-project laravel/laravel project_name
Create a database, and edit the .env file with your database name inside your project_name folder
Here is my database name: newproject
DB_DATABASE=newproject
Run this command for create model, resource controller with migration file.
php artisan make:model Model_Name -mcr
Config the route file (web.php)
Route::resource('/url',ControllerName::class);
Config the database file
$table->string('name');
$table->string('email')->unique();
$table->string('phone');
Config the controller
public function index()
{
$data = Student::all();
return view('index',compact('data'));
}
public function create()
{
return view('create');
}
public function store(Request $request)
{
Student::create($request->all());
}
public function show(Student $student)
{
return view('show',compact('student'));
}
public function edit(Student $student)
{
return view('edit',compact('student'));
}
public function update(Request $request, Student $student)
{
if($student->update($request->all())){
return redirect('/student');
}
}
public function destroy(Student $student)
{
if($student->delete()){
return redirect('/student');
}
}
Config model
use HasFactory,;
protected $fillable = [
'name',
'email',
'phone',
];
Create view template inside resource folder, then inside view.
create.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
<form action="{{ route('student.store') }}" method="POST">
@csrf
<input type="text" name="name" class="form-control my-2" placeholder="Name">
@error('name')
{{ $message }}
@enderror
<input type="text" name="email" class="form-control my-2" placeholder="Email">
@error('email')
{{ $message }}
@enderror
<input type="text" value="{{ old('phone') }}" name="phone" class="form-control my-2" placeholder="Phone">
@error('phone')
{{ $message }}
@enderror
<button type="submit" class="btn btn-primary">Add Student</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</html>
edit.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
<form action="{{ route('student.update',$student->id) }}" method="post">
@csrf
@method('PUT')
<input type="text" value="{{ $student->name }}" name="name" class="form-control my-2" placeholder="Name">
<input type="email" value="{{ $student->email }}" name="email" class="form-control my-2" placeholder="Email">
<input type="text" value="{{ $student->phone }}" name="phone" class="form-control my-2" placeholder="Phone">
<button type="submit" class="btn btn-primary">Update Student</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</html>
index.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
<td>Manage</td>
<td>Delete</td>
</tr>
@forelse($data as $single)
<tr>
<td>{{ $single->name }}</td>
<td>{{ $single->email }}</td>
<td>{{ $single->phone }}</td>
<td>
<a href="{{ route('student.show',$single->id) }}" class="btn btn-info">View</a>
<a href="{{ route('student.edit',$single->id) }}" class="btn btn-warning">Edit</a>
</td>
<td>
<form action="{{ route('student.destroy',$single->id) }}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="5" class="text-center">No Student</td>
</tr>
@endforelse
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</html>
show.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<td>Name</td>
<td>{{ $student->name }}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $student->email }}</td>
</tr>
<tr>
<td>Phone</td>
<td>{{ $student->phone }}</td>
</tr>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</html>