【发布时间】:2019-07-25 19:55:52
【问题描述】:
我正在尝试从我的数据透视表中获取数据。
客户表:
---|------
id | name
---|------
1 | John
---|------
2 | Steve
订单表:
---|------
id | description
---|------
1 | Mac
---|------
2 | Keyboard
---|------
3 | Printer
client_order(数据透视)表:
id | client_id | order_id
---|-----------|------
1 | 1 1
---|-----------|------
2 | 1 | 2
---|-----------|------
3 | 2 | 3
客户端.php
public function orders()
{
return $this->belongsToMany('App\Order','client_order');
}
订单.php
public function clients()
{
return $this->belongsToMany('App\Client','client_order');
}
现在,我如何从数据透视表中检索数据?例如:
John | Mac, Keyboard (2 orders)
Steve| Printer (1 orders)
谢谢。
【问题讨论】:
标签: laravel many-to-many pivot-table