1

我的http://localhost:8888/VGL/public/category/18?sty=3

dd($request->sty);等于 3

但是我$request->sty输入的whereHas

未定义变量:请求

public function show(Request $request, $id)
{
    $products = Product::with('features')
        ->whereHas('features', function ($q) {
            return $q->where('id', $request->sty);
        })
        ->where('category_id',17)
        ->get();
}
4

1 回答 1

7

试试这个

如果要在内部使用任何变量,where closure则必须在内部传递该变量use($variable)

public function show(Request $request, $id)
{
    $products = Product::with('features')
        ->whereHas('features', function ($q) use($request) {
            return $q->where('id', $request->sty);
        })
        ->where('category_id',17)
        ->get();
}
于 2019-03-20T05:41:02.260 回答