如何在laraveleloquent中加入三个table?

How to join three table in laravel eloquent?

我有三个 table:

consignee (id,name) , jobs(id,consignee_id), expenses(id,job_id)

在我的ExpensesController

public function index(){
        $expenses = \App\Expense::all();
        return view('expenses.index', compact('expenses'));}

在我的索引视图页面中,我现在想在我的视图页面中获取费用 table 和收货人姓名的所有详细信息

@foreach($expenses as $expense)
<tr>
  <td width="5%">{!! $expense->id !!}</td>
<td width="7%">{!! $expense->date !!}</td>
<td width="7%">{!! $expense->job_id !!}</td>
<td width="10%">{!!$expense-> !!}</td></tr>
                        @endforeach

我不知道如何在我的索引视图中获取收货人姓名,有人给我建议吗

而不是:

$expenses = \App\Expense::all();

您可以使用:

$expenses = \App\Expense::select('expenses.*','consignee.name')
            ->leftJoin('jobs','jobs.id','=','expenses.job_id')
            ->leftJoin('consignee','consignee.id','=','jobs.consignee_id') 
            ->get();

这样你就可以与其他 2 个表进行连接并获得收货人的名称。