Skip to content

Fix collect() returning Eloquent Collection of Data objects - #1219

Open
tareq-halaby wants to merge 1 commit into
spatie:mainfrom
tareq-halaby:fix/1212-eloquent-collect-base-collection
Open

tareq-halaby wants to merge 1 commit into
spatie:mainfrom
tareq-halaby:fix/1212-eloquent-collect-base-collection

Conversation

@tareq-halaby

Copy link
Copy Markdown

Summary

Fixes #1212.

Data::collect(Model::all()) was returning Eloquent\Collection<Data>. Since Laravel 12.27, loadMissing() on that collection crashes with Call to undefined method ...Data::newQueryWithoutRelationships().

Laravel's own Collection::map() already switches Eloquent collections to a base Support\Collection once items are no longer models. This PR does the same when \$into is omitted.

Explicit collect(\$items, EloquentCollection::class) still works for callers that want the Eloquent type.

Test plan

  • New Pest coverage for default Eloquent → base Collection
  • Explicit into: EloquentCollection::class still returns Eloquent

Eloquent\Collection assumes Model items (loadMissing, etc.). Mapping to
Data objects and keeping the Eloquent collection type crashes callers on
Laravel 12.27+. Align with Laravel map()/toBase() unless into is explicit.

Fixes spatie#1212
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

collect() returns an Eloquent collection of Data objects, which breaks code that eager-loads on Laravel 12.27+

1 participant