Fetch Data Using One to Many Relationship in Eloquent ORM in Laravel Framework

Create new database named laravel_db. In this database, create 2 tables named category and product as below:

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

    laravel new LearnLaravelWithRealApps

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

    http://localhost:8000

Open .env file in root folder. Add values as below connect to database:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

Open Windows Terminal in Visual Studio Code and clear config cache with command as below:

php artisan config:clear

In app/Models folder, create new models as below:

Create new PHP file named Category.php in app/Models folder as below:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $primarykey = 'id';
    protected $table = 'category';
    public $timestamps = false;

    public function products()
    {
        return $this->hasMany(Product::class);
    } 
}

Create new PHP file named Product.php in app/Models folder as below:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public $primarykey = 'id';
    protected $table = 'product';
    public $timestamps = false;

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }
}

Create new folder named Repositories. In Repositories folder, create new folder named Product. Create new PHP file named ProductRepository.php in app/Repositories/Product folder as below:

<?php

namespace App\Repositories\Category;

interface CategoryRepository
{
    public function findAll();
}

Create new PHP file named CategoryRepositoryImpl.php in app/Repositories/Category folder as below:

<?php

namespace App\Repositories\Category;

use App\Models\Category;

class CategoryRepositoryImpl implements CategoryRepository
{
    public function findAll()
    {
        return Category::get();
    }
}

Open AppServiceProvider.php file in app/Providers folder and add declare new repository to register method as below:

<?php

namespace App\Providers;

use App\Repositories\Category\CategoryRepository;
use App\Repositories\Category\CategoryRepositoryImpl;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(CategoryRepository::class, CategoryRepositoryImpl::class);
    }

    public function boot()
    {
    }
}

Create new PHP file named DemoController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

use App\Repositories\Category\CategoryRepository;

class DemoController extends Controller
{
    protected $categoryRepository;

    public function __construct(CategoryRepository $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

    public function index()
    {
        $data = array(
            'categories' => $this->categoryRepository->findAll()
        );
        return view('demo/index')->with($data);
    }
}

Create new folder named demo in resources/views folder. In this folder, create new Blade file named index.blade.php as below:

<html>

    <head>
        <title>Laravel</title>
        <style type="text/css">
            table, th, td {
                border: 1px solid;
                border-collapse: collapse;
            }
        </style>
    </head>

    <body>

    <h3>Index</h3>
        <table border="1">
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
            @foreach($categories as $category)
                <tr>
                    <td>{{$category->id}}</td>
                    <td>{{$category->name}}</td>
                </tr>
                <tr>
                    <td colspan="2" valign="top">
                        <b>Product List({{count($category->products)}})</b>
                        <table border="1" style="width: 100%">
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Price</th>
                                <th>Quantity</th>
                                <th>Status</th>
                                <th>Created</th>
                                <th>Description</th>
                            </tr>
                            @foreach($category->products as $product)
                                <tr>
                                    <td>{{$product->id}}</td>
                                    <td>{{$product->name}}</td>
                                    <td>{{$product->price}}</td>
                                    <td>{{$product->quantity}}</td>
                                    <td>{{$product->status}}</td>
                                    <td>{{$product->created}}</td>
                                    <td>{{$product->description}}</td>
                                </tr>
                            @endforeach
                        </table>
                    </td>
                </tr>
            @endforeach
        </table>

    </body>

</html>

Open web.php file in routes folder, add new routes as below:

<?php
    
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

Route::get('/', [DemoController::class, 'index']);
Route::get('/demo', [DemoController::class, 'index']);
Route::get('/demo/index', [DemoController::class, 'index']);

Access index action in Demo controller with urls as below:

  • http://localhost:8000
  • http://localhost:8000/demo
  • http://localhost:8000/demo/index

Output