Reputation: 65
I have a User model & a Sale model. A User can be either a Merchant or Customer.
When a sale is made the sale belongs to a merchant but also to a customer.
I figured I can give the sale a merchant_id & a sale_id and create 2 different relationships in the models.
Just didn't feel right to me. So my question is there a better way to go about this? I'm in a early stage of development. I wouldn't mind changing the database around a bit.
I'm using laravel 5.3
Upvotes: 1
Views: 68
Reputation: 163788
It's not one-to-one, it's many-to-many relationship and sales
table should be a pivot table. Relation usually looks like this:
class User extends Authenticatable
{
public function customers()
{
return $this->belongsToMany('App\User', 'sales', 'customer_id', 'merchant_id');
}
Where customer_id
and merchant_id
are foreign keys pointed to id
in users
table.
If you do a lot of stuff with sales
table, just keep using Sale
model for this pivot table.
Upvotes: 1