查询生成器。同时使用 Where() 和 With() 方法

Query builder. Simultaneously Where() and With() methods

HTML 表单包含多个我将它们发送到服务器

$min  = $request->input('min');
$max  = $request->input('max');
$sort = $request->input('sort');

有时输入的值为空,因此我使用查询生成器:

$q = DB::table('adverts');

if ($request->has('max') && $request->input('max') != ""){
  $q->where('price', "<" , $max)->orderBy($col, $way);
}

但是如果我添加 ->with('images') 我得到一个错误 Call to undefined method Illuminate\Database\Query\Builder::with()

 if ($request->has('max') && $request->input('max') != ""){
  $q->where('price', "<" , $max)->with('images')->orderBy($col, $way);
}

您不能将 with()DB class 一起使用,with() 应用于 Eloquent Builder。

示例:$books = App\Book::with(['author', 'publisher'])->get();.

如果要使用DB构建器,则必须使用joins加载相关数据。