Php Laravel,雄辩:从多对多关系中获取所有条目

Php Laravel,雄辩:从多对多关系中获取所有条目,php,laravel,eloquent,many-to-many,Php,Laravel,Eloquent,Many To Many,我有两个由透视表链接的表(使用自定义迁移): 利息表: ID | label 人员表: ID | label PersonHasInterest表(自定义迁移): 如何从数据透视表中获取所有记录(加入个人和兴趣)?我不想获取一个人或所有有兴趣的人的所有兴趣,但要获取数据透视表的所有条目(带联接)。尝试定义如下数据透视模型: <?php ... use Illuminate\Database\Eloquent\Relations\Pivot; ... class PersonHasIn

我有两个由透视表链接的表(使用自定义迁移):

利息表:

ID | label
人员表:

ID | label
PersonHasInterest表(自定义迁移):


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

尝试定义如下数据透视模型:

<?php

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

即使
Pivot
扩展了
Model
,也无法在
Pivot
-对象上调用标准模型函数

我想到的是使用执行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 

为数据透视表定义一个模型,然后调用
PersonHasInterest::all()?
但是我如何为数据透视定义一个模型?与为模型定义模型的方法相同,您可以指定表属性以及关系是什么?这是不可能的。您不能用这种方法实例化数据透视。看见
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