I would suggest creating a custom pivot model. There are some good instructions for how to do that here:
https://github.com/laravel/framework/issues/2093#issuecomment-39154456
Did u try something like.
// House Model
public function owners()
{
return $this->belongsToMany('Owner', 'house_owner', 'house_id', 'owner_id');
}
// Owner Model
public function houses()
{
return $this->belongsToMany('House', 'house_owner', 'owner_id', 'house_id');
}
iClosedz said:
Did u try something like. (...)
Considering your suggestion, I suppose you could add a second parameter to the belongsToMany()-relationship to indicate the name of the pivot table in plural, like so:
// In the House model
return $this->belongsToMany('Owner', 'house_owners');
Then, in the HouseOwner-model, I could drop the table-attribute, since the table name now could be plural. I still consider this configuration, though.
kayladnls said:
I would suggest creating a custom pivot model. There are some good instructions for how to do that here:
https://github.com/laravel/framework/issues/2093#issuecomment-39154456
Remembering back to when I had a similar discussion on the Kohana forum, some people argued that once you put extra data into a pivot table, it isn't a pivot table anymore but, rather, represents an independent model in, and of itself. I guess it's a question of your database structure which solution you could go for here.
In some cases, you could have a many-to-many relationship between a pivot table and another model. In such a case, I'd treat the pivot models just like any other model - separate entities where I specify the relationships manually. As I said, it comes down to the complexity of the database and you preferences.
Thanks for your answers!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community