Laravel 10 CRUD Operation With YouTube Video Tutorial

  • 21-02-2023
  • 1367
  • Laravel 10
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Hello artisan, today in this post we will see the laravel 10 crud operation from scratch, In this post, you will learn the laravel 10 crud operation from scratch and step-by-step guidelines. We will see the laravel crud operation for beginners, I will give you a simple example of the crud operation in laravel 10.

This Laravel 10 CRUD operation covers all the basic details of this latest version. as we all know that in laravel 10 CRUD operation let us know all the details about new updates and a basic understanding of the latest version.

In this CRUD (create, read, update, delete) we will take an example of a Product, we will create a new fresh laravel 10 project and will make create a product, read the stored data from the database, update that product, and will delete that product.

So let's start step by step with learn laravel 10 crud operation with an example, you can also follow the youtube video at the end of the post.

Laravel 10 CRUD operation Steps :

  • Step 1. Install Laravel 10.
  • Step 2. Configuration Database.
  • Step 3. Create Products Table Migration.
  • Step 4. Product Model.
  • Step 5. ProductController.
  • Step 6. Product Create Blade File..
  • Step 7. Define Routes.
  • Step 8. Start Local Server.

Step 1. Install Laravel 10

First, we will create a new fresh laravel app, so let's start by entering the below command in the terminal and install. skip this step you already installed the laravel 10 version.

Laravel Upgrading To 10.0 From 9.x Version

composer create-project laravel/laravel example-app

How to Install Laravel 10 With Video Tutorial

Step 2. Configuration Database

.env

Once the project is installed, we will config the app database, goto phpMyAdmin and create a database and configuration here in your .env file. this file you will see on the root of the application. If you don't see it, there is a ".env.example" you will see just copy and rename ".env".

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel10
DB_USERNAME=root
DB_PASSWORD=

Step 3. Create Products Table Migration

We will create a Product model, migration, and controller. use the suggested below command and create it.

php artisan make:model Product -mcr

We will add some columns in the products table, just copy the given product migration.

database/migrations/2023_02_19_041138_create_products_table.php
<?php

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

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

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

Step 4. Product Model

Now we will ad that field name in the product model, and add that field in the fillable array attribute.

app/Models/Product.php
<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;

    protected $fillable = ['name','detail'];
}

Step 5. ProductController

In this ProductController we will add, index(), create(), store(), show(), edit(), update(), and destroy(), just copy This controller and paste it into your production controller.

app/Http/Controllers/ProductController.php
<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {
        $products = Product::latest()->paginate(5);

        return view('products.index',compact('products'))->with('i',(request()->input('page',1) - 1) * 5);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): View
    {
        return view('products.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);

        Product::create($request->all());
        return redirect()->route('products.index')->withSuccess('Product created successfully.');
    }

    /**
     * Display the specified resource.
     */
    public function show(Product $product): View
    {
        return view('products.show',compact('product'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Product $product): View
    {
        return view('products.edit',compact('product'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Product $product): RedirectResponse
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);

        $product->update($request->all());
        return redirect()->route('products.index')->withSuccess('Product updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Product $product): RedirectResponse
    {
        $product->delete();

        return redirect()->route('products.index')->withSuccess('Product deleted successfully.');
    }
}

Step 6. Product Create Blade File.

Now create a create.blade.php file in the "resources/views/products/create.blade.php" path. just copy the given code and paste into your blade file.

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

Create an edit file in the "resources/views/products/edit.blade.php" path. In this file, we will create an edit product form code. copy this code and paste into your edit.blade.php file.

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

Create an index.blade.php file, this file is for listing the product from the database, we will show product create list table here, also we will add action in the tale for the edit and destroy the product.

resources/views/products/index.blade.php
@extends('products.layout')

@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Laravel 10 CRUD Example From Scratch - Webappfix</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-success" href="{{ route('products.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 ($products as $product)
<tr>
    <td>{{ ++$i }}</td>
    <td>{{ $product->name }}</td>
    <td>{{ $product->detail }}</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

We will create a layout file, The all blade file will extend here in this main blade file. We will extend all the blade files in this file.

resources/views/products/layout.blade.php
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

    <title>Laravel 10 CRUD Application - Webappfix</title>
  </head>
  <body>
    <div class="container">
    	@yield('content')
    </div>
  </body>
</html>
</pre>
<strong class="text-danger">resources/views/products/show.blade.php</strong>
<pre class="prettyprint">
@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

Step 7. Define Routes

Define the route in the route resource, use the below route resource code, and paste that route into your route file.

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ProductController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group that
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::resource('products', ProductController::class);

Step 8. Start Local Server

Now start the development server, use the below command, and start your app's local server.

php artisan serve

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :