viper75x's avatar

Model vs Pivot

I've come to a conundrum.

I have an Items table and a Bins table.

The Bins table has five main fields: Id, fk1, fk2, fk3, fk4. FK is an alias (for simplicity's sake) for four different foreign keys that make up the Bin - which, when combined, make up the storage location.

An Item can have many Bins, stored in more than one location. A Bin can have many Items.

This tells me it's a many to many relation.

The third table, bin_item was created as a standard model, so I had a factory, seeder, etc. I could seed the table with data...

Now, as I'm going through the relations to make sure everything can relate to the other proper entities, I find that I am not sure what to do to this third table. If I change it to extend Pivot rather than Model, how would this affect the Controller, Factory, and Seeder?

If I leave it as a Model, is it possible to use it as a Pivot table?

Thank you for your help in advance.

0 likes
1 reply
bugsysha's avatar

I always use extends Pivot if that table is pivot table. Haven't had any issues with controllers, factories and seeders, but I don't know how do you use pivot models. If you look at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Pivot.php you will see that the Pivot extends Model so you should be good. Also I haven't used and Pivot models in Laravel 8 for now so I'm not sure if something will cause any issues there since for Laravel 8 factories changed.

Please or to participate in this conversation.