'Laravel 8 how to call child relationship
i have a 2 table for relationship. Product as parents and ProductImage as child . in this case i want to show ProductImage when im doing loop foreach data in blade. her emy code
this is Product model :
public function images()
{
return $this->hasMany(ProductImage::class, 'product_id','id');
}
this is ProductImages as child model :
public function products()
{
return $this->belongsTo(Product::class, 'product_id','id');
}
This is my blade views for showing data
@foreach ($products as $product)
<tr>
<td>{{$loop->iteration++}}</th>
<td><img src="{{ asset('images/'.$product->images->image_name) }}" class="rounded d-block" width="150px"></td>
<td>{{$product->product_name}}</td>
<td>
@endforeach
and this is my ProductController :
$products = Product::with('images')->orderBy('id','desc')->get();
when i try to do <img src="{{ asset('images/'.$product->images->image_name) }}" , it's returning error like this Property [image_name] does not exist on this collection instance. `
so my summary question is, how do i call children class in my blade?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
