How to make this PHP code pass the php-cs-fixer? - php-cs-fixer

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

Related

Having trouble passing variable through Controller, into Eloquent Query

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.

Is there any way to pass variable in rememberForever callback?

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 (...) { ... }

Is the Smarty function register_modifier() necessary?

My code:
index.php
function smarty_function_eightball($params, $smarty)
{
$answers = array('Да',
'Нет',
'Никоим образом',
'Перспектива так себе...',
'Спросите позже',
'Все может быть');
$result = array_rand($answers);
return $answers[$result];
}
function smarty_modifier_capitalize($string)
{
return ucwords($string);
}
index.tpl
{eightball|capitalize}
The code works fine. Why then would I need the function register_modifier()?
That is to bind PHP functions as Smarty variable modifiers.
It has no use to bind functions that already exist in Smarty of course.
Since capitalize is already build into Smarty, {eightball|capitalize} works without using register_modifier().

how to use Route::input in laravel4?

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');
});

Codeigniter mimic where_in() functionality with a simple where() statement

I have a method which can take a variable $where. This is then passed to a $this->db->where($where); statement.
I am trying to mimic the functionality of where_in() for one particular function.
I have a list of IDs in either array format or imploded string format.
I have tried passing $where=array('blog.ID IN'=>'1,3');
to the method to no avail.
This is causing WHEREblog.IDIN '1,3'
to be output instead of WHEREblog.IDIN '1','3'
Can anyone advise how i can use codeigniters where() function to mimic what its where_in() function does?
Thanks
Try:
$array = [1,3];
$where='blog.ID IN' . join(',', $array);
Couldn't you do something like this?
function your_db_thingie($where)
{
$this->db->select('*');
if (is_array($where))
{
$this->db->where_in('field', $where);
}
else
{
$this->db->where('field', $where);
}
return $this->db->get('database');
}

Resources