Why don't you do that in the database instead, it's faster and uses less resources?
Dec 27, 2024
3
Level 63
Filter a collection
Hello,
I have this collection.
Illuminate\Database\Eloquent\Collection {#1749 ▼ // app/Services/SaleService.php:24
#items: array:1 [▼
0 =>
App\Models
\
Commission {#1752 ▼
#connection: "mariadb"
#table: "commissions"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:13 [▼
"id" => 1
"slug" => "01JG43DNA3EGY9Q583ZX7PSS1C"
"from" => "2024-12-27"
"to" => "2024-12-27"
"role" => "seller"
"calculation_base" => "1100.00"
"amount" => "215.00"
"sale_id" => 1
"user_id" => 7
"company_id" => 1
"created_at" => "2024-12-27 13:03:18"
"updated_at" => "2024-12-27 13:30:27"
]
#original: array:13 [▶]
#changes: []
#casts: array:4 [▶]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:9 [▶]
#guarded: array:1 [▶]
}
]
#escapeWhenCastingToString: false
}
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
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.