664 0 0 0
Last Updated : 2025-04-28 22:07:31
In this snippet I will teach you how to access relationship data in laravel blade files with where condition
Hello guys,
In this snippet I will teach you how to get relationship data in laravel blade file with where condition
First
you have to define the relation in your model relatioship like this
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Project extends Model
{
use HasFactory;
##--------------------------------- RELATIONSHIPS
public function images() {
return $this->hasMany(Image::class, 'project_id', 'id');
}
public function cover() {
return $this->images()->where('cover', '1'); // this is the where condition you want
}
}
Second
In you blade file just access that cover image like below
<img src="{{ asset('storage/projects') }}/{{$project->cover[0]->name}}" class="img-fluid mb-3">
// NOTE THAT - to access that relationship use {{$project->cover[0]->name}}