user14399919
user14399919

Reputation:

Undefined variable $status even though it should work (according to my logic)

I have this piece of code in one of my files

$status = $this->request->get('status');
            if ($status !== "0") {
                $query = Student::whereHas('statusuri', function($q) {
                    $q->where('stare_stagiu_id', '=', $status);
                })->get();
            }  

and I get "undefined variable $status" and I can't understand why.

From my POV, it should 100% work, but I might be missing something.

Why is this error thrown?

Thanks.

Upvotes: 0

Views: 163

Answers (1)

mynd
mynd

Reputation: 794

In your closure function($q) you need to inject $status by utilizing use statement:

 $query = Student::whereHas('statusuri', function($q) use ($status) {
                $q->where('stare_stagiu_id', '=', $status);
            })->get();

Upvotes: 3

Related Questions