Ad
How To Join Three Table In Laravel Eloquent?
I have three tables:
consignee (id,name) , jobs(id,consignee_id), expenses(id,job_id)
In my ExpensesController
public function index(){
$expenses = \App\Expense::all();
return view('expenses.index', compact('expenses'));}
in my index view page i want get all the details in expenses table with consignee name now in my view page
@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
i dont know how to get consignee name in my index view any one suggest me idea
Ad
Answer
Instead of:
$expenses = \App\Expense::all();
you can use:
$expenses = \App\Expense::select('expenses.*','consignee.name')
->leftJoin('jobs','jobs.id','=','expenses.job_id')
->leftJoin('consignee','consignee.id','=','jobs.consignee_id')
->get();
This way you make join with 2 other tables and get name of Consignee.
Ad
source: stackoverflow.com
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key
Ad