Products CRUD Example In Laravel 10 Industry Best Practice

Table of Contents

Introduction:

Laravel is a PHP framework that has become quite popular in recent years. One of the reasons for its popularity is that it follows industry best practices, including the use of Model-View-Controller (MVC) architecture, which separates business logic, presentation, and data handling. In this blog post, we will be creating a simple example of CRUD (Create, Read, Update, Delete) operations for a Product model using Laravel’s industry best practices.

We will be using Laravel 10 for this example. Let’s get started.

Step 1: Create a new Laravel project

Create a new Laravel project using the following command:

				
					composer create-project --prefer-dist laravel/laravel product-crud-example
				
			

Step 2: Create a Product model

In Laravel, a model represents a database table. We will create a Product model to represent our products table.

				
					php artisan make:model Product -mcr
				
			

This command will create a Product model with the following files:

  • app/Models/Product.php
  • database/migrations/2023_03_25_000000_create_products_table.php
  • app/Http/Controllers/ProductController.php
  • routes/web.php

The -mcr option will also create a migration, controller, and resourceful routes for the Product model.

Step 3: Define the products table schema

Open database/migrations/2023_03_25_000000_create_products_table.php file and define the schema for the products table.

				
					id();
            $table->string('name');
            $table->text('description');
            $table->decimal('price', 8, 2);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('products');
    }
}
				
			

This schema defines the idnamedescriptionprice, and timestamps columns for the products table.

Step 4: Define the Product model

Open the app/Models/Product.php file and define the Product model.

				
					<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'description',
        'price',
    ];
}
				
			

The Product model extends the base Model class and uses the HasFactory trait. The $fillable property defines which model attributes are mass assignable.

Step 5: Define Custom Request Classes

Create a new StoreProductRequest class using the following command:

				
					php artisan make:request StoreProductRequest
				
			

Open the app/Http/Requests/StoreProductRequest.php file and define the StoreProductRequest.

				
					 'required|string|max:255',
            'description' => 'required|string',
            'price' => 'required|numeric|min:0',
        ];
    }
}
				
			

Create a new UpdateProductRequest class using the following command:

				
					php artisan make:request UpdateProductRequest
				
			

Open the app/Http/Requests/UpdateProductRequest.php file and define the UpdateProductRequest.

				
					 'required|string|max:255',
            'description' => 'required|string',
            'price' => 'required|numeric|min:0',
        ];
    }

}
				
			

Step 6: Define the Product controller

Open the app/Http/Controllers/ProductController.php file and define the ProductController.

				
					validated());

        return redirect()->route('products.index');
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  UpdateProductRequest  $request
     * @param  Product  $product
     * @return RedirectResponse
     */
    public function update(UpdateProductRequest $request, Product $product): RedirectResponse
    {
        $product->update($request->validated());

        return redirect()->route('products.index');
    }

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

        return redirect()->route('products.index');
    }
}
				
			

Step 7: Define the Product Views

Create the `products` directory inside the `resources/views` directory. Create the following views inside the `products` directory:

create files:
– index.blade.php
– create.blade.php
– edit.blade.php

index.blade.php

				
					@extends('layouts.app')

@section('content')
    <h1>Products</h1>

    <p>
        <a href="{{ route('products.create') }}">Create Product</a>
    </p>

    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>Price</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($products as $product)
                <tr>
                    <td>{{ $product-&gt;name }}</td>
                    <td>{{ $product-&gt;description }}</td>
                    <td>{{ $product-&gt;price }}</td>
                    <td>
                        <a href="{{ route('products.edit', $product) }}">Edit</a>

                        
                            @csrf
                            @method('DELETE')

                            <button type="submit">Delete</button>
                        
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
@endsection
				
			

The index.blade.php view displays a table with all the products in the database. It also provides links to create a new product and edit or delete existing products.

create.blade.php

				
					@extends('layouts.app')

@section('content')
    <h1>Create Product</h1>

    
        @csrf

        <div>
            <label for="name">Name:</label>
            
        </div>

        <div>
            <label for="description">Description:</label>
            <textarea name="description" id="description">{{ old('description') }}</textarea>
        </div>

        <div>
            <label for="price">Price:</label>
            
        </div>

        <button type="submit">Create</button>
    
@endsection
				
			

The create.blade.php view displays a form to create a new product. It includes fields for the name, description, and price of the product.

edit.blade.php

				
					@extends('layouts.app')

@section('content')
    <h1>Edit Product</h1>

    
        @csrf
        @method('PATCH')

        <div>
            <label for="name">Name:</label>
            name) }}" required&gt;
        </div>

        <div>
            <label for="description">Description:</label>
            <textarea name="description" id="description">{{ old('description', $product-&gt;description) }}</textarea>
        </div>

        <div>
            <label for="price">Price:</label>
            price) }}" step="0.01" required&gt;
        </div>

        <button type="submit">Update</button>
    
@endsection
				
			

The edit.blade.php view displays a form to edit an existing product. It pre-populates the fields with the current values of the product and updates the product in the database when the form is submitted.

Step 8: Define the product routes

				
					Route::get('/products', [ProductController::class, 'index'])-&gt;name('products.index');
Route::get('/products/create', [ProductController::class, 'create'])-&gt;name('products.create');
Route::post('/products', [ProductController::class, 'store'])-&gt;name('products.store');
Route::get('/products/{product}/edit', [ProductController::class, 'edit'])-&gt;name('products.edit');
Route::patch('/products/{product}', [ProductController::class, 'update'])-&gt;name('products.update');
Route::delete('/products/{product}', [ProductController::class, 'destroy'])-&gt;name('products.destroy');
				
			

These routes map the HTTP methods and URIs to the methods defined in the ProductController class.

Step 9: Run Project

				
					php artisan serve
				
			

you get something like this:

				
					INFO Server running on [http://127.0.0.1:8000].
				
			

Conclusion:

In this blog post, we have learned how to implement the CRUD operations for a Product model in Laravel. We have followed the industry best practices to create a Request class to validate the input, a Model class to represent the database table, a Controller class to handle the HTTP requests, and views to display and process the input. We have also defined the routes to map the HTTP methods and URIs to the methods in the controller. This is just a basic example, but it can be extended and customized to fit your specific needs.

Popular Post

Leave a Reply

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