Skip to content

Conversation

@bezhermoso
Copy link

Ran into an issue when trying to refresh an Eloquent collection that contains a mixture of types. The original implementation uses the first element of the collection to hydrate the rest of the records' array into fresh models, and unfortunately that means that will fail eventually.

This is the result of a failing test I wrote to illustrate this:

1) Nanigans\SingleTableInheritance\Tests\SingleTableInheritanceTraitCollectionTest::testRefresh
Nanigans\SingleTableInheritance\Exceptions\SingleTableInheritanceException: Cannot construct newFromBuilder for unrecognized type=bike

/Users/bezalel/Development/single-table-inheritance/src/SingleTableInheritanceTrait.php:195
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:363
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:362
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2132
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:625
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:609
/Users/bezalel/Development/single-table-inheritance/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:391
/Users/bezalel/Development/single-table-inheritance/tests/SingleTableInheritanceTraitCollectionTest.php:41

I propose introducing this trait that can be used in a custom Eloquent builder attached to models that use single-table inheritance. See what's done in the Vehicle model fixture and the VehicleBuilder class.

@bezhermoso
Copy link
Author

Hi @jonspalmer, was hoping you can take a look at this when you get the chance?

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.

1 participant