Ambika Blog

Laravel 10 CRUD Application Tutorial for Beginners

Published on January 26, 2025 | Author: Shubham Parmar

In this tutorial, i would like to show you laravel 10 crud operation example. we will implement a laravel 10 crud application for beginners. i will give you simple example of how to create crud in laravel 10. you will learn crud operation in laravel 10.

Step 1: Install Laravel 8

First of all, we need to get a fresh Laravel 8 version application using the below command. So, open your terminal or command prompt and run the following command:

    composer create-project --prefer-dist laravel/laravel blog
Copied!

Step 2: Database Configuration

In the second step, we will configure the database, such as the database name, username, password, etc., for our CRUD application in Laravel 8. So, let's open the `.env` file and fill in the details as shown below:

    
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=here your database name (e.g., blog)
    DB_USERNAME=here database username (e.g., root)
    DB_PASSWORD=here database password (e.g., root)
            
Copied!

Step 3: Create Migration

We are going to create a CRUD application for products. So, we have to create a migration for the "products" table using the Laravel 8 `php artisan` command. First, run the following command:

    php artisan make:migration create_products_table --create=products
Copied!

After running this command, you will find a migration file in the following path: `database/migrations`. You have to put the following code in your migration file to create the `products` table:

    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateProductsTable extends Migration
    {
        public function up()
        {
            Schema::create('products', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->text('detail');
                $table->timestamps();
            });
        }
    
        public function down()
        {
            Schema::dropIfExists('products');
        }
    }
            
Copied!

Now you have to run this migration with the following command:

Step 4: Add Resource Route

Here, we need to add a resource route for the product CRUD application. So, open your "routes/web.php" file and add the following route:

routes/web.php

    
    use App\Http\Controllers\ProductController;
    Route::resource('products', ProductController::class);
            
Copied!

Step 5: Controller and Model

In this step, we will create a new controller called `ProductController`. So run the following command to create the new controller with resource methods:

    php artisan make:controller ProductController --resource --model=Product
Copied!

After running this command, you will find a new file in the following path: `ProductController.php`.

ProductController.php

    
    namespace App\Http\Controllers;
    
    use App\Models\Product;
    use Illuminate\Http\Request;
    
    class ProductController extends Controller
    {
        public function index()
        {
            $products = Product::latest()->paginate(5);
            return view('products.index', compact('products'))
                ->with('i', (request()->input('page', 1) - 1) * 5);
        }
    
        public function create()
        {
            return view('products.create');
        }
    
        public function store(Request $request)
        {
            $request->validate([
                'name' => 'required',
                'detail' => 'required',
            ]);
            Product::create($request->all());
            return redirect()->route('products.index')
                ->with('success', 'Product created successfully.');
        }
    
        public function show(Product $product)
        {
            return view('products.show', compact('product'));
        }
    
        public function edit(Product $product)
        {
            return view('products.edit', compact('product'));
        }
    
        public function update(Request $request, Product $product)
        {
            $request->validate([
                'name' => 'required',
                'detail' => 'required',
            ]);
            $product->update($request->all());
            return redirect()->route('products.index')
                ->with('success', 'Product updated successfully');
        }
    
        public function destroy(Product $product)
        {
            $product->delete();
            return redirect()->route('products.index')
                ->with('success', 'Product deleted successfully');
        }
    }
            
Copied!

Ok, so after running the above command, you will find "app/Models/Product.php" and put the following content in the Product.php file:

app/Models/Product.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'
        ];
    }
                
Copied!

Step 6: Add Blade Files

In this step, we will create blade files. First, we create a layout file, then a "products" folder with CRUD-related blade files.

1) layout.blade.php

2) index.blade.php

3) create.blade.php

4) edit.blade.php

5) show.blade.php

Let's create the following file and put the code below:

products/layout.blade.php

    <!DOCTYPE html>
    <html>
    <head>
        <title>Laravel 8 CRUD Application - ItSolutionStuff.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>
                
Copied!

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 8 CRUD Example from scratch - ItSolutionStuff.com</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
Copied!

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
    
Copied!

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
    
Copied!

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
    
Copied!

Now we are ready to run our crud application example with laravel 8 so run bellow command for quick run:

    php artisan serve
Copied!

Now you can open bellow URL on your browser:

    http://localhost:8000/products
Copied!