Laravels object_get() helper broken in PHP7 - laravel-4

We are working on an older Laravel 4.2 project and the new environment it is going on requires PHP7. We are trying to get this working, but we notice that the object_get() helper does not seem to work properly. Is there anything in this code that would not work from PHP 5.6 to 7?
function object_get($object, $key, $default = null)
{
if (is_null($key) || trim($key) == '') return $object;
foreach (explode('.', $key) as $segment)
{
if ( ! is_object($object) || ! isset($object->{$segment}))
{
return value($default);
}
$object = $object->{$segment};
}
return $object;
}

Related

Make authorization Laravel

Im got error in section elseif, i want make HOD looking only at its own department data
public function index(User $user)
{
if(auth()->user()->role == 'Admin')
{
$form = Form::all();
}
elseif(auth()->user()->positions == 'HOD')
{
$form = Form::all()->department('user_id', \Auth::user()->id)->get();
}
else
{
$form = Form::where('user_id', \Auth::user()->id)->get();
}
return view('form.index', ['list_form' => $form]);
}
what should i change in elseif code ?
Try to do a dd() on auth()->user()->positions if it returns nothing, the relation between User model and Positions doesnt exist, or is set up wrong.

Function create_function() is deprecated in php7.2

Getting
Function create_function() is deprecated Error in laravel 5.4 and php 7.2.
Not able to find solution.
please see below code
public static function delimiterToCamelCase($string, $delimiter = '[\-\_]')
{
// php doesn't garbage collect functions created by create_function()
// so use a static variable to avoid adding a new function to memory
// every time this function is called.
static $callback = null;
if ($callback === null) {
$callback = create_function('$matches', 'return strtoupper($matches[1]);');
}
return preg_replace_callback('/' . $delimiter . '(\w)/', $callback, $string);
}
please help.
create_function has been deprecated as of php 7.2. Instead you can use anonymous_function.
if ($callback === null) {
$callback = function ($matches) {
return strtoupper($matches[1]);
};
}

In Laravel how do i create a query string?

This will be obvious to someone else
I have a route that works and goes to the correct controller
Route::get('v1/holidays/{country}/{year}/{month}/{official?}',
'retrieveHolidayController#test'
so if i go to
http://example.com/v1/holidays/US/2014/03/01
it will go where I want to go
however I want the link to look like
http://example.com/v1/holidays?country=US&year=2014&month=03&official=01
How can I do this please ?
You redefine your route to
Route::get('v1/holidays', 'retrieveHolidayController#test');
Then in your controller you can get the param values with $request
public function test(Request $request)
{
if ( $request->has('country') && $request->country != '') {
$country = $request->country;
}
if ( $request->has('year') && $request->year != '') {
$year = $request->year;
}
.... // and the others. Then you can query like this
$holidays = Holiday::when($country, function($query) use ($country) {
return $query->where('country', $country);
})
->when($year, function($query) use ($year) {
return $query->where('year', $year);
})
->get();
//Using 'when' only executes the closure if the variable exists
}
Now, you can use your URL just the way you wanted: http://example.com/v1/holidays?country=US&year=2014&month=03&official=01
Make country,year and monthalso optional:-
Route::get('v1/holidays/{country?}/{year?}/{month?}/{official?}', 'retrieveHolidayController#test')
Route::get('v1/holidays', 'retrieveHolidayController#test');
Route::get('v1/holidays/{country}/{year}/{month}/{official?}', function($country){
return redirect()->to(action('retrieveHolidayController#test', ["country"=>$country,......]));
});
access to http://example.com/v1/holidays/US/2014/03/01
redirect to http://example.com/v1/holidays?country=US&year=2014&month=03&offical=01
if official param is null redirect param offical= param is nullable
not feel good
so
isset($official){
$paramArry["official"] = $official;
}

Yii2 ActiveRecord cache

How to use ActiveRecotd cache for Yii 2? I did't find any examples in official docs. In Google I found 2 examples, first is:
$db = self::getDb();
$object = $db->cache(function ($db) use($id) {
return self::findOne($id);
});
But it doesn't work for Model, I tested with updated framework. Other example is:
$data = \Yii::$app->cache->get('some_var_' . $id);
if ($data === false)
{
$data = self::findOne($id);
\Yii::$app->cache->set('some_var_' . $id, $data, 60);
}
It's working fine, but it's not ActiveRecord caching it's data caching, So we haven't got ActiveRecord caching in Yii 2?
1) Use cache like that:
$db = Yii::$app->db;// or Category::getDb()
$result = $db->cache(function ($db) use ($id) {
return Category::find()->where(['id' => $id])->all();
}, CACHE_TIMEOUT);
2) If you may use query dependency, use like that:
$db = Yii::$app->db;// or Category::getDb()
$dep = new DbDependency();
$dep->sql = 'SELECT count(*) FROM category';
$result = $db->cache(function ($db) use ($id) {
return Category::find()->where(['id' => $id])->all();
}, CACHE_TIMEOUT, $dep);
I too am having trouble with this. Here's my workaround for the time being for a hasOne() relationship.
public function getGroup()
{
if(isset(static::$_getGroup[$this->id])) {
return static::$_getGroup[$this->id];
}
$Group = $this->hasOne(BillChargesGroup::className(), ['id' => 'group_id'])->one();
static::$_getGroup[$this->id] = $Group;
return $Group;
}
I only want to cache data for the current request, so this works. However because I'm using ->one(); it does not return the ActiveQuery object if we call $model->getGroup() (which I found is good for extending queries)
Unfortunately if I do return the ActiveQuery object, Yii2 does some "magic" on it and always does a SELECT * which I can't control.
Since 2.0.14 you can use the following shortcuts:
(new Query())->cache(7200)->all();
// and
User::find()->cache(7200)->all();
Source: https://www.yiiframework.com/doc/guide/2.0/en/caching-data

Handle CodeIgniter remapping with parameters

I have the following code I added to a MY_Controller that is being extended by all my controllers:
public function _remap($method, $params = array())
{//exit($this->router->fetch_class());
if (array_search($method, $this->private_methods) !== false && !$this->logged_in)
{
$this->session->set_flashdata('message', array(
'message' => 'You must login to access the requested area',
'error' => 1
)
);
redirect('/');
}
else if (method_exists($this, $method))
{
$this->$method($params);
}
else
{
redirect('/');
}
}
The issue being created is that the call $this->$method($params) is condensing the params in to an Array. So a method such as the following breaks:
function some_method($param1, $param2, $param3)
Is there a way to break this array back into individual items for functions like this?
I was trying to do the same and find the same problem with
$this->$method($params);
I found another option
call_user_method_array($method,$this,$params);
Which worked but is already deprecated.
Function in PHP deprecated, what should I use now?
But hopefully that is the new one.
call_user_func_array(array($this,$method), $params);
I have tried this and it worked for me
public function _remap($method,$params = array())
{
if (method_exists($this, $method)) {
if($method == 'delete_photo' || $method == 'delete_video'){
call_user_func_array(array($this,$method), $params);
}
else{
$this->$method();
}
}
else {
$this->index($method);
}
}
Now you just have to replace "delete_photo" & "delete_video" with your methods names that contains parameters, and of course you can add as much methods as you want.

Resources