Use the query this way
select c.*, i.sgs, i.tpl, i.kasko, i.inter_permis
from cars as c
left join insur_docs as i on i.car_id = c.id
Domanda
I have 2 tables "cars" and "insur_docs" and a query that joins 2 tables and send the query data to a function which is called in my view. But the problem is that I want to display all cars from table cars not only cars from join. So if a car has no id in table "insur_doc" to display empty column "status" and if the car has id at table "insur_doc" to display result that get from function that I have crated. controller
$result = DB::select('select c.*, i.sgs, i.tpl, i.kasko, i.inter_permis from cars as c inner join insur_docs as i where i.car_id = c.id');
$date = Carbon::now();
$limit_date = Carbon::now()->addMonths(1);
return View::make('pages.index', array(
'result' => $result,
'date' => $date,
'limit_date' => $limit_date
));
}
view
@foreach ($result as $r)
<tr>
<td>{{ $r->id }}</td>
<td>
{{ link_to_action('CarController@show',$r->Make, $r->id)}}</td>
{{ Form::open(array('action' => 'CarController@show', $r->id)) }}
{{ Form::close() }}
<td>{{ $r->License }}</td>
<td>{{ $r->Milage }}</td>
<td>
@if( Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) >= 0 && (Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) <= 4))
{{ HTML::image("assets/img/success_20.png", "Success") }}
@elseif ( Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) >= 5 && (Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) <= 20))
{{ HTML::image("assets/img/warning_20.png", "Warning") }}
@else
{{ HTML::image("assets/img/danger_20_1.png", "Danger") }}
@endif
</td>
</tr>
@endforeach
I am displaying only values that get from join, but I also want to display other cars and status column to be empty.
Soluzione
Use the query this way
select c.*, i.sgs, i.tpl, i.kasko, i.inter_permis
from cars as c
left join insur_docs as i on i.car_id = c.id