Step 4: Building Product Management

Step 4: Building Product Management (CRUD Operations)

In our previous post (Step 3), we designed our database schema and created the models with their relationships. Now it’s time to build the product management system the heart of any e-commerce site.


📝 What You’ll Build

In this step, we’ll develop a complete CRUD (Create, Read, Update, Delete) system for products:

  • An admin panel where you can:
    • Add new products
    • Edit existing products
    • Delete products
    • View a list of all products
  • Each product will include:
    • Name, description, price, stock quantity
    • Product image
    • Category association

⚙️ Step 1: Create ProductController

Use Artisan to generate the controller:

php artisan make:controller ProductController --resource

This creates a resourceful controller with all seven(7) methods (index, create, store, show, edit, update, destroy).


⚙️ Step 2: Define Routes

Open routes/web.php and register resource routes for products:

use App\Http\Controllers\ProductController;

Route::resource('products', ProductController::class);

This automatically creates routes for all CRUD actions.


⚙️ Step 3: Building the ProductController Logic

Here’s a sample structure:

namespace App\Http\Controllers;

use App\Models\Product;
use App\Models\Category;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index()
    {
        $products = Product::with('category')->latest()->paginate(10);
        return view('products.index', compact('products'));
    }

    public function create()
    {
        $categories = Category::all();
        return view('products.create', compact('categories'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name'=>'required',
            'price'=>'required|numeric',
            'stock'=>'required|integer',
            'image'=>'nullable|image|max:2048',
            'category_id'=>'required'
        ]);

        $path = null;
        if ($request->hasFile('image')) {
            $path = $request->file('image')->store('products','public');
        }

        Product::create([
            'name'=>$request->name,
            'description'=>$request->description,
            'price'=>$request->price,
            'stock'=>$request->stock,
            'image'=>$path,
            'category_id'=>$request->category_id,
        ]);

        return redirect()->route('products.index')->with('success','Product created!');
    }

    public function edit(Product $product)
    {
        $categories = Category::all();
        return view('products.edit', compact('product','categories'));
    }

    public function update(Request $request, Product $product)
    {
        $request->validate([
            'name'=>'required',
            'price'=>'required|numeric',
            'stock'=>'required|integer',
            'image'=>'nullable|image|max:2048',
            'category_id'=>'required'
        ]);

        if ($request->hasFile('image')) {
            $path = $request->file('image')->store('products','public');
            $product->image = $path;
        }

        $product->update($request->except('image'));

        return redirect()->route('products.index')->with('success','Product updated!');
    }

    public function destroy(Product $product)
    {
        $product->delete();
        return redirect()->route('products.index')->with('success','Product deleted!');
    }
}

⚙️ Step 4: Create Blade Views

You will need:

  • resources/views/products/index.blade.php
  • resources/views/products/create.blade.php
  • resources/views/products/edit.blade.php

Here’s a simple starting point for the index page:

@extends('layouts.app')

@section('content')
<div class="container">
  <h1 class="mb-3">All Products</h1>
  <a href="{{ route('products.create') }}" class="btn btn-primary mb-3">Add New Product</a>

  @if(session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
  @endif

  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Name</th><th>Category</th><th>Price</th><th>Stock</th><th>Image</th><th>Actions</th>
      </tr>
    </thead>
    <tbody>
      @foreach($products as $product)
        <tr>
          <td>{{ $product->name }}</td>
          <td>{{ $product->category->name ?? 'N/A' }}</td>
          <td>${{ $product->price }}</td>
          <td>{{ $product->stock }}</td>
          <td>
            @if($product->image)
              <img src="{{ asset('storage/'.$product->image) }}" width="60">
            @endif
          </td>
          <td>
            <a href="{{ route('products.edit',$product) }}" class="btn btn-sm btn-warning">Edit</a>
            <form action="{{ route('products.destroy',$product) }}" method="POST" style="display:inline">
              @csrf @method('DELETE')
              <button class="btn btn-sm btn-danger">Delete</button>
            </form>
          </td>
        </tr>
      @endforeach
    </tbody>
  </table>

  {{ $products->links() }}
</div>
@endsection

✅ Summary

At this stage, you now have:

  • A fully functional CRUD system for products
  • A clean admin interface to manage them
  • Products stored with images and related to categories

This is the core feature of your e-commerce platform. Next, we will build the frontend shop page to display these products to customers.

Leave a Comment