Collection::groupBy should not return a new static() because that might be an EloquentCollection #53298
Replies: 4 comments 6 replies
-
I wonder if this is related to my issue #53283 |
Beta Was this translation helpful? Give feedback.
-
Funny, I came across a similar thing earlier when I was trying to write a docblock for the output. Larastan also apparently does some magic with trying to infer the type. larastan/larastan#1860 In my opinion, there's nothing that stops an EloquentCollection from containing things other than Models (other than good sense). It seems a bit inconvenient to convert this to a base Collection for a case like this. // Get all orders that have shipped
$orders = Order::where('status', 'shipped')->get();
// Group them by the shipper
$shipperToOrdersCollection = $orders->groupBy('shipper');
foreach($shipperToOrdersCollection as $shipperName => $ordersCollection) {
// Check the status of the shipment
$shippingStatus = ShipperApi::for($shipperName)->getShippingStatus();
// Update all of the orders with the latest status
$ordersCollection->toQuery()->update(['status' => $shippingStatus]);
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Opened this PR (#53304) to hopefully solve my |
Beta Was this translation helpful? Give feedback.
-
Laravel Version
11.23.5
PHP Version
8.2.14
Description
Currently, a collection
groupBy()
returns the same type of collection, containing the same type of collection:@return static<array-key, static<array-key, TValue>>
. That's good for the base collection, of children that accept any type ofTValue
, but not forEloquentCollection
, because that doesn't accept any type of TValue, but onlyModel
:@template TModel of \Illuminate\Database\Eloquent\Model
. SogroupBy()
should check for that (orEloquentCollection
could overwritegroupBy()
) to not returnnew static
, butnew Illuminate\Support\Collection
.Steps To Reproduce
Fixing might be super backward incompatible, but it is correcter. Workaround is pretty simple though: add in custom model collection (which everyone has, right?):
Beta Was this translation helpful? Give feedback.
All reactions