Hello,
I’m building a website with a database of albums, each album has a title, a country and an artist (or several), a label (or several).
For the artist and the label, I’m using in a pages fields where you select the label and the artist into a list of pages (once you have created them).
The problem come when I try to use Search() in my collection of albums :
$filteredItems->search($search, 'title|artist|label|city')
It doesn’t seems to search into the label and the artist, because they are a collection of pages. I saw on several forum topics that we couldn’t do much more with the search() function. So do I need to add something like this for my research ? Or is there an another way ?
$filteredItems = $albums->filterBy(function($album) use ($search) {
return Str::contains($album->label()->title(), $search);
});
This last bit doesn’t seems to work either.
Thank you so much for you time.
Have a nice day.
Did you use a controller for your search?
/site/controllers/search.php
With the solution in the cookbook, my search worked on the first try. It is very easy to implement:
Indeed, $items->search($search, 'title|artist|label|city')
will only search the literal content of your content files here. And since you say that label and artist are pages fields, this will be just the (u)uid of the selected pages, not their title or content.
Your idea seems to be in the right direction, but you probably have to turn your fields into actual pages (see the ->toPage()
addition):
$items = $albums->filterBy(function($album) use ($search) {
return
Str::contains($album->title(), $search) ||
Str::contains($album->artist()->toPage()->title(), $search) ||
Str::contains($album->label()->toPage()->title(), $search) ||
Str::contains($album->city(), $search);
});
Yes, but it doesn’t work with pages fields 
Yes ->toPage(
) was the missing part for it to work, thank you so much 
I added something for the uppercase/lowercase, and it works perfectly.
Thank you for your time !
1 Like