FindBy() in a structure collection

Hello,

I have a collection of structure objects built like this:

$allFairs = page('fairs')->fairs()->toStructure()->sortBy('openingdate','desc')

And I was trying to merrily find a structure object in it, by using findby() and a field in the structure objects of that collection:

$allFairs->findBy('name','aname')

…that does not seem to work, I assume because findby() is a $pages method and this is not exactly a $pages collection ?

If I am correct and I cannot use findBy is there any method I can use that works like findBy() but for a collection of structure objects? ( other than looping $allFairs ).

Thank you

findBy() is a collection method that also works with structure collections. Please post your structure field definition.

Yes I’ve been mendling with it and confirmed it working, the problem was with several structure items sharing the same value in their ‘name’ field.

Thanks

findBy() finds exactly one item, i.e. the first one that matches the given argument.
To find multiple items that match an argument, use filterBy().

What I actually needed was to find an item by multiple fields. Or, actually, to know if such item existed in the collection.

In the end I moved the logic in the collection to the page’s markup, as it was using a simple V::date(), the collection was kind of superfluous.

Thanks !