Laravel-未定义的变量:请求

Hao*_*ung 1 get-request laravel undefined-variable

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

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

但是我把$request->stywhereHas

未定义的变量:请求

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();
}
Run Code Online (Sandbox Code Playgroud)

bha*_*njr 6

尝试这个

如果您想在中使用任何变量,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();
}
Run Code Online (Sandbox Code Playgroud)

  • 很高兴为您提供帮助。您可以根据需要接受作为答案。因此其他开发人员可以轻松找到答案 (2认同)