如果不在连接查询中,则显示列为空

时间:2014-05-12 14:52:43

标签: php laravel

我有2张桌子"汽车"和" insur_docs"和一个连接2个表并将查询数据发送到我视图中调用的函数的查询。但问题是我想要从桌面车展示所有汽车,而不仅仅是来自加入的汽车。因此,如果一辆汽车在表格中没有id" insur_doc"显示空列"状态"如果汽车在桌子上有id" insur_doc"显示从我已创建的功能获得的结果。 控制器

$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
    ));
}

视图

 @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

我只显示来自加入的值,但我还希望将其他汽车和状态列显示为空。

1 个答案:

答案 0 :(得分:1)

以这种方式使用查询


    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