Laravel Eloquent Query -> Undefined Variable - php

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();

Related

passed variable in nested where query in laravel 9

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.

how can pass function as argument php class

I want to write a class to submit using the function method and get access to example_method at the class
$users = User::where('posts', function($q){
$q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;
As I understand - you want to have access to $q variable in anonymous function, you can access it this way:
$users = User::where('posts', function() use ($q) {
$q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;
In case you want access variable defined outside function you have to inherit it using use.
More info: https://www.php.net/manual/en/functions.anonymous.php

Access to out-of-scope variable within anonymous functions in Laravel (PHP)

I want to seed the related tables in Laravel. I had a problem in access to the out-of-scope variable inside the anonymous function which I had defined for whereHas methods to put "where" conditions on my has queries.
$id = $user->id; // out-of-scope variable
$posts = Post::whereHas('comments', function ($query) {
$query->where('user_id', $id);
})->get();
Technically I don't have access to $id inside the anonymous function.
This isn't a Laravel question, but a PHP one. Just add use ($variable) after the parameter list:
$posts = Post::whereHas('comments', function ($query) use ($id) {
$query->where('user_id', $id);
})->get();

Laravel Advanced Wheres how to pass variable into function?

Example in doc:
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
But what if I need to use external variable like that:
->where('city_id', '=', $this->city->id)
->where(function($query)
{
$query->where('name', 'LIKE', '%'.$searchQuery.'%')
->orWhere('address', 'LIKE', '%'.$searchQuery.'%')
})
For now I created new property and accessed it through $this->, but is there any more convenient way?
You can pass the necessary variables from the parent scope into the closure with the use keyword.
For example:
DB::table('users')->where(function ($query) use ($activated) {
$query->where('activated', '=', $activated);
})->get();
More on that here.
EDIT (2019 update):
PHP 7.4 (will be released at November 28, 2019) introduces a shorter variation of the anonymous functions called arrow functions which makes this a bit less verbose.
An example using PHP 7.4 which is functionally nearly equivalent (see the 3rd bullet point below):
DB::table('users')->where(fn($query) => $query->where('activated', '=', $activated))->get();
Differences compared to the regular syntax:
fn keyword instead of function.
No need to explicitly list all variables which should be captured from the parent scope - this is now done automatically by-value. See the lack of use keyword in the latter example.
Arrow functions always return a value. This also means that it's impossible to use void return type when declaring them.
The return keyword must be omitted.
Arrow functions must have a single expression which is the return statement. Multi-line functions aren't supported at the moment. You can still chain methods though.
#kajetons' answer is fully functional.
You can also pass multiple variables by passing them like: use($var1, $var2)
DB::table('users')->where(function ($query) use ($activated,$var2) {
$query->where('activated', '=', $activated);
$query->where('var2', '>', $var2);
})->get();
If you are using Laravel eloquent you may try this as well.
$result = self::select('*')
->with('user')
->where('subscriptionPlan', function($query) use($activated){
$query->where('activated', '=', $roleId);
})
->get();
You can pass variables using this...
$status =1;
$info = JOBS::where(function($query) use ($status){
$query->where('status',$status);
})->get();
print_r($info);

Using variable in laravel 4.1 Query Builder

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.

Categories