Using Laravel's query builder example is it possible to pass a variable to this function:
$someVariable = 1;
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) use ($someVariable) {
$query->where('votes', '>', $someVariable)
->where('title', '<>', 'Admin');
})
->get();
It seems the function cannot access the variable outside of itself. I get an error: Undefined variable: $someVariable
You'll need to use the "use" keyword after your function, for variables outside of that function. If $someVariable
is the one you want to use, this should work.
$someVariable = 1;
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) use($someVariable) {
$query->where('votes', '>', $someVariable)->where('title', '<>', 'Admin');
})->get();
use Superglobals
$GLOBALS["someVariable"] = 1;
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', $GLOBALS["someVariable"])
->where('title', '<>', 'Admin');
})
->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.