Menu Close

Complete Crud functionality in Laravel 9

We suppose that Laravel framework is installed and running.

We are going to create an entity called ‘School’, We will create database table by migration, model class, views and how controller will work, so lets start!

Laravel is giving terminal functionality to create code much easier. I am going to use Gitbash terminal here to perform laravel commands

Step1) Create Migration and Model by using following one command

php artisan make:model School -mcr

It will create three files. One migration file will be in /database/migrations path, model will be at app/Models/School.php and controller in app/Http/Controllers/SchoolController.php

Update up() function in migration file, for example

public function up()
{
Schema::create('school', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->string('website')->nullable();
$table->string('detail')->nullable();
$table->timestamps();
});
}

and school model need to update as following

class School extends Model
{
use HasFactory;
protected $table = 'school';
protected $fillable = ['name', 'address', 'city', 'country' , 'phone' , 'email' , 'website' , 'detail'];

}

please make sure that we are going to create singular table name ‘school‘.

Now run migrate command to generate table in database

php artisan migrate

SchoolController.php can be updated as following

<?php

namespace App\Http\Controllers;

use App\Models\School;
use Illuminate\Http\Request;

class SchoolController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$schools = School::orderBy('id','desc')->paginate(5);
return view('school.index', compact('schools'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('school.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',
'address' => 'required',
'city' => 'required',
'country' => 'required',
'phone' => 'required',
'email' => 'required',
'website' => 'required',
'detail' => 'required',
]);

School::create($request->post());

return redirect()->route('school.index')->with('success','School has been created successfully.');
}

/**
* Display the specified resource.
*
* @param \App\Models\School $school
* @return \Illuminate\Http\Response
*/
public function show(School $school)
{
return view('school.show',compact('school'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\School $school
* @return \Illuminate\Http\Response
*/
public function edit(School $school)
{
return view('school.edit',compact('school'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\School $school
* @return \Illuminate\Http\Response
*/
public function update(Request $request, School $school)
{
$request->validate([
'name' => 'required',
'address' => 'required',
'city' => 'required',
'country' => 'required',
'phone' => 'required',
'email' => 'required',
'website' => 'required',
'detail' => 'required',
]);

$school->fill($request->post())->save();

return redirect()->route('school.index')->with('success','School Has Been updated successfully');
}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\School $school
* @return \Illuminate\Http\Response
*/
public function destroy(School $school)
{
$school->delete();
return redirect()->route('school.index')->with('success','School has been deleted successfully');
}
}

Create Views files

  1. create folder name ‘school’ in resources/views
  2. Create 3 blade view files as following
    • create.blade.php
    • edit.blade.php
    • index.blade.php

please update these views like

index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>School</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>School List</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-success" href="{{ route('school.create') }}"> Create School</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<thead>
<tr>
<th>S.No</th>
<th>School Name</th>
<th>Email</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Phone</th>
<th width="280px">Action</th>
</tr>
</thead>
<tbody>
@foreach ($schools as $school)
<tr>
<td>{{ $school->id }}</td>
<td>{{ $school->name }}</td>
<td>{{ $school->email }}</td>
<td>{{ $school->address }}</td>
<td>{{ $school->city }}</td>
<td>{{ $school->country }}</td>
<td>{{ $school->phone }}</td>
<td>
<form action="{{ route('school.destroy',$school->id) }}" method="Post">
<a class="btn btn-primary" href="{{ route('school.edit',$school->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
{!! $schools->links() !!}
</div>
</body>
</html>

create.blade.php

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Add School Form</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>

<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left mb-2">
<h2>Add School</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('school.index') }}"> Back</a>
</div>
</div>
</div>
@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
{{ session('status') }}
</div>
@endif
<form action="{{ route('school.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>School Name:</strong>
<input type="text" name="name" class="form-control" placeholder="School Name">
@error('name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Address:</strong>
<input type="text" name="address" class="form-control" placeholder="Address">
@error('address')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>City:</strong>
<input type="text" name="city" class="form-control" placeholder="City">
@error('city')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Country:</strong>
<input type="text" name="country" class="form-control" placeholder="Country">
@error('country')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Phone:</strong>
<input type="text" name="phone" class="form-control" placeholder="Phone">
@error('phone')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>School Email:</strong>
<input type="email" name="email" class="form-control" placeholder="School Email">
@error('email')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Website:</strong>
<input type="text" name="website" class="form-control" placeholder="Website">
@error('website')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Detail:</strong>
<input type="text" name="detail" class="form-control" placeholder="Detail">
@error('detail')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
</div>
</body>

</html>

edit.blade.php

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Edit School Form</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>

<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit School</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('school.index') }}" enctype="multipart/form-data">
Back</a>
</div>
</div>
</div>
@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
{{ session('status') }}
</div>
@endif
<form action="{{ route('school.update',$school->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>School Name:</strong>
<input type="text" name="name" class="form-control" placeholder="School Name" value="{{ $school->name }}">
@error('name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Address:</strong>
<input type="text" name="address" class="form-control" placeholder="Address" value="{{ $school->address }}">
@error('address')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>City:</strong>
<input type="text" name="address" class="form-control" placeholder="City" value="{{ $school->city }}">
@error('city')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Country:</strong>
<input type="text" name="address" class="form-control" placeholder="Country" value="{{ $school->country }}">
@error('country')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Phone:</strong>
<input type="text" name="address" class="form-control" placeholder="Phone" value="{{ $school->phone }}">
@error('phone')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Email:</strong>
<input type="email" name="email" class="form-control" placeholder="School Email"
value="{{ $school->email }}">
@error('email')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Website:</strong>
<input type="text" name="address" class="form-control" value="{{ $school->website }}" placeholder="Website">
@error('website')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Detail:</strong>
<input type="text" name="address" value="{{ $school->detail }}" class="form-control"
placeholder="Detail">
@error('detail')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
</div>
</body>

</html>

So the last thing we are going to set routing URL

open routes/web.php file add following code

use App\Http\Controllers\SchoolController;
Route::resource('school', SchoolController::class);

Thats it!

its time to test feature, please run laravel by following command

php artisan serve

and browse http://127.0.0.1:8000/school for testing.

Please comment if you face any kind of issue, we will try best to answer asap.

Leave a Reply

Your email address will not be published. Required fields are marked *