<?php
Class TDSRouting {
public $routing = [
[
'os' => 'android',
'url' => '/url/to/redirect'
],
[
'os' => 'android',
'browser' => 'chrome',
'country' => 'ru',
'type' => 'mobile',
'url' => '/url/to/redirect2'
]
];
public $default = '/url/default';
public function redirect($options = []){
$keys = array_keys($options);
foreach($this->routing as $route){
$diff = array_diff_assoc($options, $route);
if(count(array_intersect_key(array_flip($keys), $route)) === count($keys) && empty($diff)){
return $route['url'];
}
}
return $this->default;
}
}
$routing = new TDSRouting;
print_r($routing->redirect([
'os' => 'android',
'browser' => 'chrome',
'country' => 'ru',
'type' => 'mobile'
]));
public function scopeFilter($query, $parameters = []){
$fields = ['visa_type', 'status', 'company'];
$filter = collect([
'visa_type' => null,
'status' => null,
'company' => null,
'paginate' => 40,
'order' => 'id',
'sort' => 'desc'
])->merge($parameters);
if (!Auth::user()->hasRole('administrator|Manager')) {
$query->where('user_id', Auth::user()->id);
}
foreach($filter->only($fields) as $field => $value){
$query->where($field, $value);
}
return $query
->orderBy($filter->get('order'), $filter->get('sort'))
->paginate($filter->get('paginate'))
->toArray()['data'];
}
SomeModel::where(...)->filter([
'visa_type' => '....',
'paginate' => 20,
'sort' => 'asc'
]);