Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ivanleoncz69's avatar

Undefined variable: request in Laravel query

I want to create a nested parameter grouping in my query where the value of one of my nested where is gotten from the form.

if($request->view != '') {
    $query->where(function ($query) {
    $query->where('type', $request->view)
            ->orWhere('type', 2);
    })->get();
}

Right now I'm getting Undefined variable: request. How do I get the value of $request->view in the function?

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@ivanleoncz69

Try this-

if($request->view != '') {
    $query->where(function ($query) use($request) { // <-- without use, your 
      $query->where('type', $request->view)         //     anonymous function
            ->orWhere('type', 2);                   //     cannot access $request
    })->get();
}
9 likes

Please or to participate in this conversation.