可以说,我有一个orders
是要有一个多到许多关系到三个名为其他表的表typings
,translates
和theses
。我知道数据透视表应该以某种方式与多对多关系建立联系,但这并不是我要寻找的。我应该如何实现数据透视表?
您将通过名为的数据透视表与订单和其他三个表创建多态关系 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');
}
}
然后,您可以得到如下模型的订单:
$thesis = Thesis::find(1);
$orders = $thesis->orders;
反之:
$order = Order::find(1);
$theses = $order->theses;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句