2

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();
}

1 Answer 1

9

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I forget to inject into the query function
Glad to help you. you can Accept as the answer if you want. so other developers find answer easily

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.