2017-07-14 161 views
0

我有一个数据透视表连接(使用自定义的迁移)两个表抓取所有条目:Laravel,雄辩:从多到许多关系

利息表:

ID | label 

Person表:

ID | label 

PersonH​​asInterest表(自定义迁移):

InterestID | PersonID | notes 

如何从数据透视表中获取所有记录(加入个人和兴趣)?我不想获得一个人或所有有兴趣的人的所有兴趣,但是数据透视表的所有条目(包含连接)。

+0

定义数据透视表的模型,然后调用'PersonH​​asInterest ::所有()' –

+0

,但怎么可以?为枢轴定义模型? –

+0

与模型相同,您可以指定表格属性 –

回答

0

即使Pivot扩展Model,不可能叫上Pivot -object标准模型的功能。 Issue on Github

我想出了使用DB-Facade执行SELECT语句,就像这样:

DB::table('person_has_interest') 
    ->join('interest', 'person_has_interest.interest_id', '=', 'interest.id') 
    ->join('person', 'person_has_interest.person_id', '=', 'person.id') 
    ->get(); // further manipulation like select possible 
0

尝试这样定义枢轴模式:

<?php 

... 
use Illuminate\Database\Eloquent\Relations\Pivot; 
... 
class PersonHasInterest extends Pivot 
{ 
    protected $table = '...'; // table name 
} 

然后使用它:PersonHasInterest::all();

+0

这是不可能的。你不能用这种方法实例化数据透视表。请参阅https://github.com/laravel/framework/issues/17770 –