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
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.
I have this $query Function in my Controller. It works. But when i try to add a variable inside it, it's not working.
this is my Full function
$setting = DB::table('settings')->where('id','=','1')->get();
foreach($setting as $st)
$date1 = $st->date1;
$date2 = $st ->date2;
$accounts = Account::with(['subaccount' => function($query){
$query->with(['subaccountinvoice'=> function($query){
$query->where('validate', '=','1')->whereBetween('updated_at', [$date1, $date2]);}])->get();
}])->get();
Variables $date1 and $date2 are not defined because they are insidd Query Function
In order to inherit a variable within a closure in PHP you need to pass it to the closure using the use keyword.
Change your accounts code to this:
$accounts = Account::with(['subaccount' => function($query) use ($date1, $date2) {
$query->with(['subaccountinvoice'=> function($query) use ($date1, $date2) {
$query->where('validate', '=','1')->whereBetween('updated_at', [$date1, $date2]);}])->get();
}])->get();
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 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();
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.