Laravel 8 : step by step CRUD operation in laravel 8

Today I am explaining you step by step CRUD operation in laravel 8. In this guide, you can learn how to perform CRUD operation in Laravel 8.

CRUD means four basic functionality like Create, Read, Update, Delete. It is the operations perform in the database. I am going to show you step by step through scratch so you can learn better understanding if you are new to learn laravel.

Step 1: Install Laravel 8

For the Laravel 8 CRUD generator process, first you need to install Laravel 8 with the help of the following command. I understand you already installed laravel 8.

composer create-project laravel/laravel:^8.0 Laravel_installation

Step 2: Database configuration

Add database setting .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(Laravel_installation)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password

Step 3: Create Migration

we are creating crud application for productdetails. so we have to create migration for “productdetails” table using Laravel 8 php artisan command, so first run below command:

php artisan make:migration create_productdetails_table --create=productdetails

After this command you will find one file under following path “database/migrations” and you have to put below code in your migration file for create productdetails table.

filename – 2023_06_18_083404_create_productdetails_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductdetailsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('productdetails', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('detail');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('productdetails');
    }
}

Now you have to run this migration command:

php artisan migrate

Step 4: Add Resource Route

we need to add resource route for productdetails crud application. so open your “routes/web.php” file and add following route.

filename – routes/web.php

use App\Http\Controllers\ProductdetailsController;
  
Route::resource('productdetails', ProductdetailsController::class);

Step 5: Add Controller and Model

In this step, now we should create new controller as ProductdetailsController. So run below command and create new controller. below controller for create resource controller.

php artisan make:controller ProductdetailsController --resource --model=Productdetail

after run this command you will find new controller file called “app/Http/Controllers/ProductdetailsController.php“. below code will available in this file.

<?php

namespace App\Http\Controllers;

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

class ProductdetailsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Productdetail  $productdetail
     * @return \Illuminate\Http\Response
     */
    public function show(Productdetail $productdetail)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Productdetail  $productdetail
     * @return \Illuminate\Http\Response
     */
    public function edit(Productdetail $productdetail)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Productdetail  $productdetail
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Productdetail $productdetail)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Productdetail  $productdetail
     * @return \Illuminate\Http\Response
     */
    public function destroy(Productdetail $productdetail)
    {
        //
    }
}

I have updated ProductdetailsController.php controller file for those functions like index, create, store , show, edit, update and destroy.

ProductdetailsController.php

<?php

namespace App\Http\Controllers;

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

class ProductdetailsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $productdetails = Productdetail::latest()->paginate(5);
    
        return view('productdetails.index',compact('productdetails'))
            ->with('i', (request()->input('page', 1) - 1) * 5);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('productdetails.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',
            'detail' => 'required',
        ]);
    
        Productdetail::create($request->all());
     
        return redirect()->route('productdetails.index')
                        ->with('success','Product details created successfully.');
    }

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Productdetail  $productdetail
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Productdetail $productdetail)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
    
        $productdetail->update($request->all());
    
        return redirect()->route('productdetails.index')
                        ->with('success','Product updated successfully');
    }

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

after updation of controller file now need to update model file. it will available “app/Models/Productdetail.php” and put below content in Productdetail.php file:

Productdetail.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Productdetail extends Model
{
	protected $table = 'productdetails'; 
    use HasFactory;
  
    protected $fillable = [
        'name', 'detail'
    ];
}

Step 6: Add Blade Files

we have to create blade files under resources\views folder. firstly create layout file and then create new folder “productdetails” then create blade files for CRUD . So you have to create following below blade file:

we have to create those files under resources\views folder like layout.blade.php, index.blade.php, create.blade.php, edit.blade.php, show.blade.php

layout.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>webdeveloperindia.in | Laravel 8 CRUD Application</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>

index.blade.php

@extends('productdetails.layout')
 
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>webdeveloperindia.in | Laravel 8 CRUD Application</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('productdetails.create') }}"> Create New Product</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>No</th>
            <th>Name</th>
            <th>Details</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($productdetails as $productdetails)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $productdetails->name }}</td>
            <td>{{ $productdetails->detail }}</td>
            <td>
                <form action="{{ route('productdetails.destroy',$productdetails->id) }}" method="POST">
   
                    <a class="btn btn-info" href="{{ route('productdetails.show',$productdetails->id) }}">Show</a>
    
                    <a class="btn btn-primary" href="{{ route('productdetails.edit',$productdetails->id) }}">Edit</a>
   
                    @csrf
                    @method('DELETE')
      
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
  
   
      
@endsection

create.blade.php

@extends('productdetails.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('productdetails.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('productdetails.store') }}" method="POST">
    @csrf
  
     <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Product 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

edit.blade.php

@extends('productdetails.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('productdetails.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('productdetails.update',$productdetail->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>Product Name:</strong>
                    <input type="text" name="name" value="{{ $productdetail->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">{{ $productdetail->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

show.blade.php

@extends('productdetails.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('productdetails.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>Product Name:</strong>
                {{ $productdetail->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Details:</strong>
                {{ $productdetail->detail }}
            </div>
        </div>
    </div>
@endsection

Now we are ready to run CRUD example with laravel 8 so run below command:

php artisan serve

Now you need open below URL on browser:

http://127.0.0.1:8000/productdetails

Listing page.

Create page.

Edit page.

show page.

Leave a Reply

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

79 − = 73