first of all, we need to get fresh Laravel 9 version application using the bellow command, So open your terminal OR command prompt and run the bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Database Configuration
In second step, we will make a database configuration for the example database name, username, password, etc for our crud application of laravel 9. So let’s open .env file and fill in all details like as below:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)
Step 3: Create Migration, Model and Controller
php artisan make:model Pad -mrc
After this command you will find one file in following path “database/migrations” and you have to put bellow code in your migration file for create products table.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pads', function (Blueprint $table) {
$table->id();
$table->integer('number');
$table->string('weight');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pads');
}
};
Now you have to run this migration by following command:
php artisan migrate
Step 4: Add Resource Route
Here, we need to add resource route for product crud application. so open your “routes/web.php” file and add following route.
routes/web.php
use App\Http\Controllers\PadController;
Route::resource('pad', PadController::class);
In this controller will create seven methods by default as bellow methods:
1)index()
2)create()
3)store()
4)show()
5)edit()
6)update()
7)destroy()
So, let’s copy bellow code and put on PadController.php file.
app/Http/Controllers/PadController.php
<?php
namespace App\Http\Controllers;
use App\Models\Pad;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PadController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$all = Pad::all();
return view('admin.pad.list')->with(['all' => $all]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.pad.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'number' => 'required',
'weight' => 'required',
],[
'number.required' => 'Pad number is required',
'weight.required' => 'Pad weight is required',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$pad = Pad::create(
[
'number' => $request->number,
'weight' => $request->weight,
]);
if($pad){
return redirect()->route('pad.index')->with('message', 'New Pad Added Successfully!');
}
return redirect()->route('pad.index')->with('message', 'Some thing went wrong!');
}
/**
* Display the specified resource.
*
* @param \App\Models\Pad $pad
* @return \Illuminate\Http\Response
*/
public function show(Pad $pad)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Pad $pad
* @return \Illuminate\Http\Response
*/
public function edit(Pad $pad)
{
return view('admin.pad.edit')->with(['pad' => $pad]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Pad $pad
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Pad $pad)
{
$validator = Validator::make($request->all(), [
'number' => 'required',
'weight' => 'required',
],[
'number.required' => 'Pad number is required',
'weight.required' => 'Pad weight is required',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$pad->number = $request->number;
$pad->weight = $request->weight;
if($pad->save()){
return redirect()->route('pad.index')->with('message', 'Pad Updated Successfully!');
}
return redirect()->back()->with('message', 'Some thing went wrong!');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Pad $pad
* @return \Illuminate\Http\Response
*/
public function destroy(Pad $pad)
{
$pad->delete();
return redirect()->back()->with('message', 'Pad deleted successfully!');;
}
}
Ok, so after run bellow command you will find “app/Models/Pad.php” and put bellow content in Pad.php file:
app/Models/Pad.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Pad extends Model
{
use HasFactory;
protected $table = 'pads';
protected $fillable = ['number' ,'weight' ];
}
Step 6: Add Blade Files
In last step. In this step we have to create just blade files. So mainly we have to create layout file and then create new folder “products” then create blade files of crud app. So finally you have to create following bellow blade file:
1) layout.blade.php
2) index.blade.php
3) create.blade.php
4) edit.blade.php
Step 7: show.blade.php
So let’s just create following file and put bellow code.
resources/views/pads/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 CRUD Application - code.yoblogger.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
resources/views/pads/index.blade.php
@extends('pads.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch - code.yobloggger.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('pad.create') }}"> Create New Pad</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>Number</th>
<th>Weight</th>
<th width="280px">Action</th>
</tr>
@foreach ($products as $product)
<tr>
<td>{{ }}</td>
<td>{{ $product->name }}</td>
<td>
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $products->links() !!}
@endsection
resources/views/products/create.blade.php
@extends('products.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('products.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>Detail:</strong>
<textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
</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
resources/views/products/edit.blade.php
@extends('products.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('products.update',$product->id) }}" method="POST">
@csrf
@method('PUT')
<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" value="{{ $product->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Detail:</strong>
<textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea>
</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
resources/views/products/show.blade.php
@extends('products.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.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>
{{ $product->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Details:</strong>
{{ $product->detail }}
</div>
</div>
</div>
@endsection
Now we are ready to run our crud application example with laravel 8 so run bellow command for quick run:
php artisan serve
Now you can open bellow URL on your browser:
http://127.0.0.1:8000/pad