Skip to content
Advertisement

Search query function on Laravel

I have an album that has images. I’m having a problem with doing the search function. I want to search for photos with the following caption. Here’s what I did so far.

Albums table:

JavaScript

Photos table:

JavaScript

and the following relations on my models:

Photo.php

JavaScript

Album.php

JavaScript

GalleryController.php

JavaScript

The search function works but if I search for a specific image within the album it returns all of the images in that album.

Advertisement

Answer

It’s not very pretty, but this is basically what you want.
I’ve commented the code to explain it.

JavaScript

Edit:
I have updated the answer to use Tim Lewis’s suggestion from the comments.
Since the closure is repeated for both steps, we can store it in a variable to keep the code DRY.

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