where where语句中的多个where

时间:2018-01-24 18:45:37

标签: laravel

我想在Laravel中生成一个查询,结果如下:

获得的结果
$results = DB::table('records')->where('email', '!=', 'test@gmail.com')->get();

执行多个where语句:

->where('id', 'like', '%'.$request->search.'%')
->orWhere('email', 'like', '%'.$request->search.'%')
->orWhere('recordType', 'like', '%'.$request->search.'%')
->orWhere('uploadDate', 'like', '%'.$request->search.'%')
->orWhere('uploadTime', 'like', '%'.$request->search.'%')
->get();

如何在控制器中用语句来表示产生所需的结果?

1 个答案:

答案 0 :(得分:2)

使用where() closure对参数进行分组:

$results = DB::table('records')->where('email', '!=', 'test@gmail.com')
    ->where(function($q) use($request) {
        $q->where('id', 'like', '%' . $request->search . '%')
          ->orWhere('email', 'like', '%' . $request->search . '%')
          ->orWhere('recordType', 'like', '%' . $request->search . '%')
          ->orWhere('uploadDate', 'like', '%' . $request->search . '%')
          ->orWhere('uploadTime', 'like', '%' . $request->search . '%');
    })
    ->get();