Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Undefined variable: request

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

When dd($request->sty); equal 3

however I put $request->sty in the whereHas is

Undefined variable: request

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();
}
like image 395
Hao Phung Avatar asked Jan 01 '26 07:01

Hao Phung


1 Answers

Try this

If you want to use any variable inside the where closure then you have to pass that variable inside the 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();
}
like image 156
bhavinjr Avatar answered Jan 03 '26 02:01

bhavinjr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!