Home > Enterprise >  passed variable in nested where query in laravel 9
passed variable in nested where query in laravel 9

Time:08-28

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

CodePudding user response:

$parents = Category::
    where('status' , 'active')
    ->where('id' , '<>' , $id)
    ->where(function($query) use ($id) { <-- problem is here
        return $query
        ->whereNull('parent_id')
        ->orWhere('parent_id', '<>', $id);
    })->get();
  • Related