I need your help. I try to do this query:
public function future_trips($cat_id, $city_id)
{
$trips=Events::where('start_date','>',Carbon::now()->subDay(5))
->where(function ($q)
{
$q->where('category_id',$cat_id)->orWhere('city', $city_id);
})
->get();
}
ErrorException: Undefined variable: cat_id in file C:\x......
Undefined variable $cat_id less... (Ctrl+F1) The inspection can
produce two types of warnings: The definition of the variable can not
be found anywhere. ("Undefined variable") There are one or more paths
to reach the line with the variable usage without defining it.
("Variable might have not been defined")
Can somebody explain me why ?
You need to add these variables to the function closure with use. Php does not automatically include variables outside the closure.
public function future_trips($cat_id, $city_id)
{
$trips = Events::where('start_date', '>', Carbon::now()->subDay(5))
->where(function ($q) use ($cat_id, $city_id)
{
$q->where('category_id',$cat_id)->orWhere('city', $city_id);
})
->get();
}
Related
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
$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();
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:
The closure is a function assigned to a variable, so you can pass it around
A closure is a separate namespace, normally, you can not access variables defined outside of this namespace. There comes the use keyword:
use allows you to access (use) the succeeding variables inside the closure.
use is early binding. That means the variable values are COPIED upon DEFINING the closure. So modifying $id inside the closure has no external effect, unless it is a pointer like an object is.
You can pass in variables as pointers like in case of &$id. This way, modifying the value of $total DOES HAVE an external effect, the original variable's value changes.
Variables defined inside the closure are not accessible from outside the closure either.
Closures and functions have the same speed. Yes, you can use them all over your scripts.
for more info read this.
Attached is a screenshot of the error I am facing. I have the log show the value of the $team variable. What am I doing wrong?
You don't have access to parents variables from a closure, you need to explicitly send variables to the closure with use keyword
function($query) use ($team)
Use forget to use $team variable inside your closure. It must be function($query) use ($team)
$data = User::whereHas('roles' => function($query) use ($team) {
$query->where('role_code', '=', $team)
})->get();
I am trying to build little complex query where everything wan OK until I tried to use a advanced where condition.
In the code below I am getting an error saying
ErrorException: Undefined variable: user in file D:...**\app\Http\Controllers\ApiAuthController.php on line 285
and the line 285 is $query->where("tbl_transactions.from_user_id", "=", $user->id) inside the orWhere()
public function loadTransactions()
{
$token = JWTAuth::getToken();
$user = JWTAuth::toUser($token);
return DB::table('tbl_transactions')
->join('tbl_users as fromu', 'fromu.id', '=', 'tbl_transactions.from_user_id')
->join('tbl_users as tou', 'tou.id', '=', 'tbl_transactions.to_user_id')
->select('tbl_transactions.id', 'tbl_transactions.from_user_id', 'tbl_transactions.to_user_id', 'tbl_transactions.amount', 'tbl_transactions.created_at', 'tbl_transactions.status', 'fromu.userid', 'tou.userid', 'tou.phone as tophone', 'fromu.phone as fromphone')
->orWhere(function ($query) {
$query->where("tbl_transactions.from_user_id", "=", $user->id)// here the problem is (undefined **$user**)
->where('tbl_transactions.to_user_id', '=', $user->id);
})
->orderBy('tbl_transactions.id', 'desc')
->take(50)
->get();
}
I am new to laravel.Anyone here could help me would be great.
You need to use the use clause to make the variable accessible in the closure scope. So, change this:
->orWhere(function ($query) {
To this:
->orWhere(function ($query) use($user) {
Im too lazy to link the other 100s of posts that are duplcates of this.
PHP manual - anonymous functions Example #3
I am doing a search to filter through users that are displayed on my screen. The display of the users is working without the search. However when I type in the search a name, and go to display it, I get an undefined variable for search.
$Search = $request->search_code; //print_r($Search);die(); // this works
// this next line was used for testing and it works as well
//$findTrainers = DB::table('users')->where('users.name', 'like', "%$Search%")->get();
// This is what we want to work, but the search variable is now undefined
$findTrainers = DB::table('users')
->join('bios', function ($findTrainers) {
$findTrainers->on('users.id', '=', 'bios.user_id')
->where('users.name', 'like', "%$Search%");
})
->get();
This is due to how closures and scoping work. Closures don't know about variables defined outside of their scope, so you must tell a closure to use that variable when you want to access it within the scope of the closure, like this:
$findTrainers = DB::table('users')
->join('bios', function ($findTrainers) use ($Search){
$findTrainers->on('users.id', '=', 'bios.user_id')
->where('users.name', 'like', "%$Search%");
})
->get();
How can use variable in laravel query builder. Here is my code.
$role = 1;
$user = DB::table('users')
->join('assigned_roles', function($join)
{
$join->on('users.id', '=', 'assigned_roles.user_id')
->where('assigned_roles.role_id', '=', $role );
})
->get();
But it return Undefined variable: role.
How can I solve this problem. Help me plz.
You need to import variables from the local scope to the anonymous function's scope:
function ($join) use ($role) {}
See the example in the docs.