Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent
Last updated 7 months ago.
0

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-...

Last updated 7 months ago.
0

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');
}
Last updated 7 months ago.
0

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-...

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!

Last updated 7 months ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

fredrkr fredrkr Joined 11 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.