我想在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();
如何在控制器中用语句来表示产生所需的结果?
答案 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();