Skip to content
Advertisement

Get all images from the database and display the images in a bootstrap 4 carousel Laravel 5.8

I’ve been trying to get all the images from the database and trying to display them in a bootstrap carousel but it is only returning one image.

JavaScript

And here is the code in the controller index function

JavaScript

When I try to change the method to get() instead of first() an error appears. I’m not sure if I need to setup a new variable for the images, or include a foreach in the view which I’m not sure how to do. Really appreciate the help.

Advertisement

Answer

JavaScript

You can access your posts like this if you are using get() method. Since first() method directly returns you the model you can access its properties but for get() you will have to loop through it since it returns “Eloquent Collection’ (not array, you can use toArray() if you need one).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement