I have a collection returned from the database through a hasMany
-relation.
$items = collect([
[
'ref' => 'efaef954',
'children' => collect([
[
'ref' => 'wetk4',
'order' => 1,
],
[
'ref' => 'wetk5',
'order' => 2,
],
[
'ref' => 'wetk6',
'order' => 3,
],
]),
],
[
'ref' => 'efgjlf954',
'children' => collect([
[
'ref' => 'wetk5',
'order' => 1,
],
[
'ref' => 'wetk6',
'order' => 2,
],
[
'ref' => 'wetk4',
'order' => 3,
],
]),
],
]);
This $items
-collection can contain any number of items with children. These children has a specific order, which is random until they are sorted.
In my user interface I can input refs of the children to get parent items that qualify. Only parent-items which have the specific order of what I input e.g. wetk4
and wetk6
should be returned.
return $items->filter(fn ($item) => $item->children->contains('wetk4') && $item->children->contains('wetk6'))
This will correctly give me items with the children, but this does not take into account the fact that the order is important in which items would qualify, considering some items will have the same children.
I've yet to find an elegant solution to this, attempting to only use collection functions alone.
wetk4
andwetk6
? (Q1) what is the desired output forwetk4
andwetk6
?, (Q2) what is the desired output forwetk6
and thenwetk4
? (Q4) what is the desired output if you have a reference that doesn't exist. e.g.wetk4
,wekt541
andwetk6
? – Croft