在laravel中的查询构建器上使用if condition error

时间:2018-03-23 11:37:05

标签: laravel if-statement query-builder

keyword发送到控制器

时,

刀片正常工作

$search_alls = DB::table($table)
                  ->where('title', '=', $keyword)
                  ->where('status','=',0)
                  ->orderBy($wanttime_by, $wanttime_order)
                  ->paginate(9);
当我不将keyword发送到控制器

时,

刀片显示错误

$search_alls = DB::table($table);
if ($keyword) {
    $search_alls->where('title', '=', $keyword);
}
$search_alls
    ->where('status','=',0)
    ->orderBy($wanttime_by, $wanttime_order)
    ->paginate(9);

刀片视图

{{ $search_all->id }}

错误消息是

  

未定义的属性:Illuminate \ Database \ MySqlConnection :: $ id

1 个答案:

答案 0 :(得分:0)

$search_alls = DB::table($table);
        if ($keyword) {
            $search_alls=$search_alls->where('title', '=', $keyword);
        }

        $search_alls=$search_alls
        ->where('status','=',0)
        ->orderBy($wanttime_by, $wanttime_order)
        ->paginate(9);

谢谢@thisiskelvin!每个人都有帮助!