I have following code,
\Cache::rememberForever('Roles', function() {
return RoleModel
::where('ParentRoleID' >= $CurrenctUserRoleID)
->get();
});
Issue is: I am getting Error
Undefined variable: CurrenctUserRoleID
Question: Is there any way to pass variable in callback?
You may try this (Notice the use of use keyword):
$CurrenctUserRoleID = 1; // Some id
\Cache::rememberForever('Roles', function() use($CurrenctUserRoleID) {
return RoleModel
::where('ParentRoleID' >= $CurrenctUserRoleID)
->get();
});
Check the PHP manual: Inheriting variables from the parent scope.
PHP.net - anonymous functions - Example #3
You aren't passing anything with the callback as you are not the caller of that callback. You are telling PHP to use a variable from the parent scope.
function (...) use (...) { ... }
Related
I'm trying to create waitlist to buy product, but change the status of the transaction from 'Confirmed' to 'Waitlist'.
For that I have added a variable 'status' and a function in Transaction Model as follows:
const CONFIRMED_TRANSACTION = 'confirmed';
const WAITLIST_TRANSACTION = 'waitlist'; //waitlist product
public function isConfirmed() {
return $this->status == Transaction::CONFIRMED_TRANSACTION;
}
Default value of Transaction->status is 'CONFIRMED_TRANSACTION'.
Now every time the Product->status changes to 'unavailable' i would like to change the value of the Transaction->status to 'WAITLIST_TRANSACTION' when the transaction is created.
I am trying to achieve it using Event Listeners as:
Transaction::created(function($transaction) {
if(!$product->isAvailable()) {
$transaction->status = Transaction::WAITLIST_TRANSACTION;
$transaction->save();
}
});
But this gives me error :
**ErrorException: Undefined variable: product in file /home/vagrant/restfulapi7/app/Providers/AppServiceProvider.php on line 29**
How can I achieve the same in a better way?
You're using what's called a lambda or anonymous function, which has a different scope to the rest of your code.
function($transaction) does not have access to a variable named $product. So you're trying to call ->isAvailable() on a variable which doesn't exist.
Transaction::created(function($transaction) {
if(!$product->isAvailable()) {
$transaction->status = Transaction::WAITLIST_TRANSACTION;
$transaction->save();
}
});
Assuming it exists in the scope in which you call Transaction::created(), you can pass the $product variable into your lambda function like this:
function($transaction) use ($product) { ... }
For more information on lambda functions, read the php docs here
This solves the issue:
if(!$transaction->product->isAvailable())
Php-cs-fixer returns the error 'braces' for one of the files. The following code causes the problem:
$meetings = Meeting::where(function ($query) use ($meeting_type_id) {
//doSomething
});
Php-cs-fixer uses the default psr1, psr2 rules (vendor/bin/php-cs-fixer fix --dry-run --verbose --format=txt).
How do i make this code pass the php-cs-fixer?
It worked by assigning the function to a variable and use this as an argument. Idkw.
$queryFunction = function ($query) use ($meeting_type_id) {
//doSomething
};
$meetings = Meeting::where($queryFunction);
I'm trying to make an ajax request along with a variable in the url of the request.
The request is supposed to retrieve results for Recipes, which has a pivot table for Flavors.
The query should retrieve all Recipes with specified flavor. I am not able to pass the $flavorID variable into the query though. Getting an undefined error.
Route::get('/api/recipes/flavor/{flavorID}', function($flavorID){
return App\Recipe::whereHas('flavors', function ($query) {
$query->where('flavor_id', $flavorID);
})->paginate(20);
});
$flavorID does not exist in the scope of the function being passed to whereHas. Use the use keyword to include the variable in the scope of the function.
Route::get('/api/recipes/flavor/{flavorID}', function($flavorID){
return App\Recipe::whereHas('flavors', function ($query) use ($flavorID) {
$query->where('flavor_id', $flavorID);
})->paginate(20);
});
See https://www.php.net/manual/en/functions.anonymous.php
When you have a sub-function, you need to pass variables into scope:
// http://example.com/api/recipies/flavor/vanilla
Route::get('/api/recipes/flavor/{flavorID}', function($flavorID){
return App\Recipe::whereHas('flavors', function ($query) use($flavorID) {
$query->where('flavor_id', $flavorID);
})->paginate(20);;
});
Adding use($flavorId) allows $flavorID to be used within function($query). If you omit it, then you get an undefined error as you're experiencing.
below 'throttle' code works good. But my question is that why we are using 'this' keyword in throttle function.
what is it actually?
please describe it.
thank you very much
$("document").ready(function(){
$("input").keypress(throttle(function(e){
$(".div1").html($("#ip").val());
},1000))
function throttle(fn,dly){
var timer=null;
return function(){
clearTimeout(timer);
timer=setTimeout(function(){
fn.apply(this,arguments);
},dly);
}
}
});
fn is a function. You can call the function in the standard way - fn() or you can call the call method or apply method on it.
When calling call or apply you have the option with the first argument to set the scope the function can be called in. The scope can also be set to null for no scope.
I am trying to use Laravel 4 method called Route:input("users"). But I am getting following error
Call to undefined method Illuminate\Routing\Router::input()
Any idea how Route::input() works. Is there any file I need to change.
Thanks all
Route::filter('userFilter', function () {
if (Route::input('name') == 'John') {
return 'Welcome John.';
}
});
Route::get('user/{name}', array(
'before' => 'userFilter',
function ($name) {
return 'Hello, you are not John.';
}));
It looks as though Route::input was added in Laravel 4.1, make sure this is the version you are working with if you need to use this functionality.
I assume you've read the docs, but since you asked how it works, here's the example:
Accessing A Route Parameter Value
If you need to access a route parameter value outside of a route, you may use the Route::input method:
Route::filter('foo', function()
{
// Do something with Route::input('users');
});