Reputation: 7895
lets say i have an orders
table that is going to have a many-to-many relation to three other tables named typings
, translates
and theses
. I know pivot table should be somehow like many to many polymorphic relation but that is not exactly what im looking for.
how should i implement pivot table?
Upvotes: 3
Views: 1321
Reputation: 13620
You would create a polymorphic relationship with orders and the other three tables through a pivot table named orderables
// TABLES NEEDED
orders
id - integer
typings
id - integer
translates
id - integer
theses
id - integer
orderables
order_id - integer
orderable_id - integer
orderable_type - string
// MODELS/RELATIONSHIPS NEEDED
class Typing extends Model
{
public function orders()
{
return $this->morphToMany('App\Order', 'orderable');
}
}
class Translate extends Model
{
public function orders()
{
return $this->morphToMany('App\Order', 'orderable');
}
}
class Thesis extends Model
{
public function orders()
{
return $this->morphToMany('App\Order', 'orderable');
}
}
class Order extends Model
{
public function typings()
{
return $this->morphedByMany('App\Typing', 'orderable');
}
public function translates()
{
return $this->morphedByMany('App\Translate', 'orderable');
}
public function theses()
{
return $this->morphedByMany('App\Thesis', 'orderable');
}
}
Then, you could get the orders of a model like this:
$thesis = Thesis::find(1);
$orders = $thesis->orders;
And the inverse:
$order = Order::find(1);
$theses = $order->theses;
Upvotes: 1