PHP Notice: Undefined variable: num in Psy Shell code on line 4 Hi
For some reason php can not read the variable $num, I get this error "PHP Notice: Undefined variable: num in Psy Shell code on line 4" when running this code:
App\Chat::where(function ($query) { $query->where('from', 1 )->where('to', $num ); })->orWhere( function ($query) { $query->where('from', $num )->where('to',1); })->where( 'from', $num )->get()
I had previously defined $num = 1;
the code works only if i put directly the number 1 in the code instead of the variable $num
So why php can not read the $num variable?
You have to use use ($num) when you pass data to the closure.
App\Chat::where(function ($query) use ($num) {
$query->where('from', 1)->where('to', $num);
})->orWhere(function ($query) use ($num) {
$query->where('from', $num)->where('to', 1);
})->where('from', $num)->get();
Bro You're wonderful. Thanks...
Please sign in or create an account to participate in this conversation.