Display hasMany relationships in a Blade File in Laravel

Do you want to Display hasMany relationships in a Blade File in the Laravel application?

hasMany relation in laravel

This step-by-step tutorial helps you learn how to display hasMany relation data in the blade file in the Laravel application with the help of the eloquent relationship.

Need to display hasMany relationship data in Laravel

Laravel hasMany and belongsTo relationships are a beautiful feature of the framework. Relationships, and specifically Eloquent Relationships, are a really popular feature of Laravel.

We use the relationship to maintain records on multiple tables with more details like we have a category and products tables, so while we add some products we define the category for related products for a category.

display hasMany records in laravel

display belongsTo records in the blade file in laravel

app\Models\MediaGalleryCategory.php

<?php

namespace App\Models;

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

class MediaGalleryCategory extends Model
{
    use HasFactory;

    public function mediaGalleryImgs()
    {
        return $this->hasMany(ImageGallery::class, 'cat_id','id');
    }
}

app\Models\ImageGallery.php

<?php

namespace App\Models;

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

class ImageGallery extends Model
{
    use HasFactory;
}

Controller


    public function index()
    {
        $mediaGalleries = MediaGalleryCategory::with('mediaGalleryImgs')->latest()->get();
        return view('media',
        ['mediaGalleries' => $mediaGalleries]);
    }

resources\views\media.blade.php

  <section class="py-5 gallery">
        <div class="container">
         @foreach ($mediaGalleries as $imageGallery)
         <div class="section-title text-center mb-5">
            <h2><span>{{ $imageGallery['category'] ?? '' }}</span></h2>
             <div class="border-line"></div>
            <div class="border-line"></div>
          </div>
          <div class="row mt-5 g-0">
            @foreach($imageGallery->mediaGalleryImgs as $media)
            <div class="col-lg-3 col-md-6">
              <div class="item overflow-hidden position-relative">
                <img src="{{ asset('storage')}}/{{ $media['image'] ?? '' }}">
              </div>
            </div>  
             @endforeach
          </div>
           @endforeach
        </div>
      </section>

I hope that this article helped you learn an example of Display hasMany relationships in a Blade File in Laravel, with the help of the eloquent relationship example. You may also want to check out our guide on Laravel Excel Export with HasOne Relationship in the Laravel application.

Related article: hasMany Relation with where condition in Laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top