I am trying to use variable to nested where() query in laravel 9 but i get an error that variable Undefined
my code:
public function edit($id)
{
$category = Category::findOrFail($id);
$parents = Category::
where('status' , 'active')
->where('id' , '<>' , $id)
->where(function($query){
return $query
->whereNull('parent_id')
->orWhere('parent_id', '<>', $id);
})->get();
}
the error:
Undefined variable $id
in this section of your code: ->where(function($query){
you need use
keyword to pass value inside this inner function as below:
->where(function($query) use ($id) { .. };
the detail for this action is:
$id
inside the closure has no external effect, unless it is a pointer like an object is.&$id
. This way, modifying the value of $total DOES HAVE an external effect, the original variable's value changes.for more info read this.