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?
@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();
}
Please or to participate in this conversation.