Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Filter a collection

Hello,

I have this collection.

And I want to filter it according to the role.

dd($sales[0]->commissions->where('role', 'seller'));

After filtering, the array is empty. I have also tried with the filter function $sales[0]->commissions->filter(...) but the result is also an empty array.

Why ?

What am I doing wrong ?

Thanks for your help.

V

0 likes
3 replies
Tray2's avatar

Why don't you do that in the database instead, it's faster and uses less resources?

1 like
vincent15000's avatar

@Tray2 Because I retrieve the $sale object from which I have several commissions (for the seller, for the setter, ...).

I wanted to add a appended property (I know that it's only for JSON responses, but I have tried), but it generates several queries.

public function getSellerCommissionAttribute()
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

@Tray2 I have solved my problem ... the role is casted as an enum, so I need to use the enum to filter the commissions.

Please or to participate in this conversation.