Get images of modules in template
-
how to get the modules images in template in foreach loop
-
Hi Meet
Assuming that you've successfully bound images to your entities and bound the collection to your view, you can loop through the collection and get images like this:
@foreach ($items as $item) @if ($item -> files() -> where("zone", "my_image") -> count()) <img src="{{ $item -> files() -> where("zone", "my_image") -> first() -> path }}"> @endif @endforeach
-
Hi Poxblossom
thanks for reply but after adding {{ $item -> files() -> where("zone", "my_image") -> first() -> path }} i am getting this error Trying to get property of non-object
-
Okay, that suggests you haven't made the data available to the view. There are a few ways you can do this, two of which are covered in this video tutorial:
-
thanks for reply but i have done this things properly but then also getting error
-
Which method are you using to get your data to the view? Please share some code.
-
I personally do this via accessors on Laravel.
If for instance you have products that can have a gallery of images I would add this in its model file:
/** * Get all the product gallery images * @return \Illuminate\Database\Eloquent\Collection */ public function getGalleryImagesAttribute() { return $this->files()->where('zone', 'gallery')->get(); }
The error you're getting
Trying to get property of non-object
without more information might be because you call->where(zone, my_image)
on null.This either your relation
$item->files()
returns null, which could be because you didn't implement theMediaRelation
trait on your model.
-
i have got your point and its working Thankyou
-
Awesome!
Don't forget to mark the topic as resolved, thank you.
-
how to mark this topic as resolved as i dont get anything in this
-
I've done it for you.
Under topic tools, you have a "mark as resolved" link.