How To Display Products By Categories Using Laravel In Php

Displaying Products by Categories using Laravel in PHP

To display products by categories using Laravel in PHP, you need to follow these steps:

  1. Create a database table to store the products with a column to store the category ID.
  2. Create a database table to store the categories.
  3. Define the relationships between the products and categories in your Laravel models.
  4. Create a database seed to populate the categories and products.
  5. Create a route in Laravel to display the products by categories.
  6. Create a controller method to handle the route and fetch the products by categories from the database.
  7. Create a view to display the products by categories.

Example:

Let’s consider an example where you have a “products” table with columns “id”, “name”, “description”, and “category_id”, and a “categories” table with columns “id” and “name”.

In your Laravel models, you will define the relationships between the products and categories. The Product model will have a belongsTo relationship with the Category model, and the Category model will have a hasMany relationship with the Product model.


  // Product.php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Product extends Model
  {
      public function category()
      {
          return $this->belongsTo(Category::class);
      }
  }
  
  // Category.php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Category extends Model
  {
      public function products()
      {
          return $this->hasMany(Product::class);
      }
  }
  

In your database seeder, you will populate the categories and products.


  // DatabaseSeeder.php
  
  namespace Database\Seeders;
  
  use Illuminate\Database\Seeder;
  use App\Models\Category;
  use App\Models\Product;
  
  class DatabaseSeeder extends Seeder
  {
      public function run()
      {
          Category::factory(5)->create()->each(function ($category) {
              $category->products()->saveMany(Product::factory(10)->make());
          });
      }
  }
  

In your Laravel routes file, you will define a route to display the products by categories.


  // web.php
  
  use Illuminate\Support\Facades\Route;
  use App\Http\Controllers\ProductController;
  
  Route::get('/products-by-category/{category}', [ProductController::class, 'productsByCategory'])->name('products.by.category');
  

In your ProductController, you will have a method to handle the route and fetch the products by categories from the database.


  // ProductController.php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Models\Category;
  
  class ProductController extends Controller
  {
      public function productsByCategory(Category $category)
      {
          $products = $category->products;
  
          return view('products_by_category', compact('products', 'category'));
      }
  }
  

In your view file, you will display the products by categories.


  // products_by_category.blade.php
  
  <h2>Products in {{ $category->name }} Category</h2>
  
  <ul>
      @foreach($products as $product)
          <li>{{ $product->name }} - {{ $product->description }}</li>
      @endforeach
  </ul>
  

By accessing the route “/products-by-category/{category}”, where “{category}” is the category ID, you will be able to see the products by categories in your view.

This is a basic example of how to display products by categories using Laravel in PHP. You can modify and enhance the code according to your specific requirements.

Same cateogry post

Leave a comment