I am doing the conditional authentication for the login so the user will redirect to their pages respectively. My code is:
protected $redirectTo = '/';
protected function redirectTo()
{
if (auth()->user()->department == 'HR' || 'Human Resource' ) {
return '/hr';
}
elseif (auth()->user()->department == 'Accountant' || 'ACC' ) {
return '/acc';
}
else return '/';
}
However, when I run the code, the page is redirected to the wrong page. How to add OR clause to the parameter?
Try This Not Use directly ||
((auth()->user()->department == 'HR') || (auth()->user()->department == 'Human Resource'))
((auth()->user()->department == 'Accountant') || (auth()->user()->department == 'ACC'))
protected function redirectTo()
{
if ((auth()->user()->department == 'HR') || (auth()->user()->department == 'Human Resource')) {
return '/hr';
}
elseif ((auth()->user()->department == 'Accountant') || (auth()->user()->department == 'ACC')) {
return '/acc';
}
else return '/';
}
Related
I Follow this error Message: Too few arguments to function Crud_model::update_course(), 1 passed in D:\OneDrive - Combina Solucoes em Tecnologia Ltda\Sites\senaec.com.br\ead\application\controllers\Admin.php on line 434 and exactly 2 expected
So I pass the 2 arguments on:
In this case the error show when a try access the followin URL: localhost/ead/courses_action/edit/2
public function course_actions($param1 = "", $param2 = "") {
if ($this->session->userdata('admin_login') != true) {
redirect(site_url('login'), 'refresh');
}
if ($param1 == "add") {
$this->crud_model->add_course();
redirect(site_url('admin/courses'), 'refresh');
}
elseif ($param1 == "edit") {
$this->crud_model->update_course($param2);
redirect(site_url('admin/courses'), 'refresh');
}
elseif ($param1 == 'delete') {
$this->crud_model->delete_course($param2);
redirect(site_url('admin/courses'), 'refresh');
}
elseif ($param1 == 'view_details') {
$page_data['page_name'] = 'course_details';
$page_data['page_title'] = get_phrase('course_details');
$page_data['course_details'] = $this->crud_model->get_course_by_id($param2)->row_array();
$this->load->view('backend/index', $page_data);
}
}
Someone can see the error in this case to fix?
Thanks!
First follow the rules of url
server/projectName/controller/function/param1/param2
You have mentioned your url is
localhost/ead/courses_action/edit/2
If 'course_action' is your function name then where is the controller. Please specify following the url pattern.
here is the validation code :
public function registration () {
//
//
// -- code
$this->form_validation->set_rules('password', 'Password', 'required|callback__check_length[6,10]');
}
function _check_length($input, $min, $max)
{
$length = strlen($input);
if ($length <= $max && $length >= $min)
{
return TRUE;
}
elseif ($length < $min)
{
$this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
return FALSE;
}
elseif ($length > $max)
{
$this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
return FALSE;
}
}
it is giving me error :
Message: Missing argument 3 for Person::_check_length(), called in C:\wamp64\www\abc\system\libraries\Form_validation.php on line 744 and defined
You can do so by exploding the 2nd param. CI doesn't seem to support 3 params in function args for form validation rules:
public function _check_length($input, $minmax) {
$minmax = explode(',', $minmax);
$min = $minmax[0];
$max = $minmax[1];
$length = strlen($input);
if ($length <= $max && $length >= $min) {
return TRUE;
} elseif ($length < $min) {
$this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
return FALSE;
} elseif ($length > $max) {
$this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
return FALSE;
}
}
You also don't have to have your own function to do this. You can simply use min_length[x] and max_length[x] rules.
https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference
I have a middleware wrapping all routes to detect the user ip, the problem is that i make the redirect to controller action and i'm getting an infinite loop with a 302 error code.
Middleware
protected $auth;
protected $userIP;
public function __construct()
{
$this->userIP = ( !empty($_SERVER["HTTP_CF_IPCOUNTRY"]) ) ? $_SERVER["HTTP_CF_IPCOUNTRY"] : false;
$this->auth = Auth::user();
if ($this->auth->guest() || $this->auth->user()->isAnonymous()) {
$this->auth = null;
}
}
public function handle($request, Closure $next)
{
if( $this->userIP ):
$country = ($this->userIP == "US") ? "USA" : (($this->userIP == "CA") ? "CANADA" : (($this->userIP == "CN") ? "CHINA" : (($this->userIP == "PA") ? "PANAMA" : (($this->userIP == "GT") ? "GUATEMALA" : "MX"))));
switch($country)
{
case "USA":
return redirect()->away('http://mysiteusa.com/');
break;
case "GUATEMALA":
return redirect()->action('Site\SwitchCountryController#update', ['key' => 8]);
break;
}
endif;
return $next($request);
}
Controller
public function update($clave)
{
$newSucursal = Sucursal::firstOnCountry($key);
if (! $newSucursal) {
return back();
}
user()->setStore($newSucursal);
return redirect('/products');
}
I'm a little lost about how to fix this, any idea?
Regards!
I have an accessor that is defined like that:
public function getNameAttribute($name)
{
return trans($name);
}
Now, I would like to add parameters to my translation, so I would like to have something like that:
public function getNameAttribute($name)
{
return trans($name, ['age' => $age]);
}
Is it possible? How should I define accessor, and how should I call send the param?
EDIT 1: Here are my model attributes:
protected $fillable = [
'id',
'name',
'gender',
'isTeam',
'ageCategory',
'ageMin',
'ageMax',
'gradeCategory',
'gradeMin',
'gradeMax',
];
And here is how I get $age variable inside my model:
public function getAgeString()
{
$ageCategoryText = '';
$ageCategories = [
0 => trans('core.no_age'),
1 => trans('core.children'),
2 => trans('core.students'),
3 => trans('core.adults'),
4 => trans('core.masters'),
5 => trans('core.custom')
];
if ($this->ageCategory != 0) {
if ($this->ageCategory == 5) {
$ageCategoryText = ' - ' . trans('core.age') . ' : ';
if ($this->ageMin != 0 && $this->ageMax != 0) {
if ($this->ageMin == $this->ageMax) {
$ageCategoryText .= $this->ageMax . ' ' . trans('core.years');
} else {
$ageCategoryText .= $this->ageMin . ' - ' . $this->ageMax . ' ' . trans('core.years');
}
} else if ($this->ageMin == 0 && $this->ageMax != 0) {
$ageCategoryText .= ' < ' . $this->ageMax . ' ' . trans('core.years');
} else if ($this->ageMin != 0 && $this->ageMax == 0) {
$ageCategoryText .= ' > ' . $this->ageMin . ' ' . trans('core.years');
} else {
$ageCategoryText = '';
}
} else {
$ageCategoryText = $ageCategories[$this->ageCategory];
}
}
return $ageCategoryText;
}
You have 2 options,
First:
If you want to get as attribute name a message translated, you can try this:
public function getNameAttribute($name)
{
return trans($name, ['age' => $this->getAgeString()]);
}
Second:
If you want a translate as additional field you can use Mutator adding an append to your model:
protected $appends = array('nameWithAge');
and define the method to get the name
public function getNameWithAgeAttribute()
{
return trans($this->attributes['name'], ['age' => $this->getAgeString()]);
}
This will do to nameWithAge an attribute more of the model and you can access it as such.
I ask the question before I was not getting anymore replies and I decide to re-post it if that will help. I hope i'm not doing anything wrong by re-posting?
$total_cost_for_A = $this->admin_model->total_cost_for_A($branch, $department, $date);
$total_cost_for_B = $this->admin_model->total_cost_for_B($branch, $department, $date);
The variables $total_cost_for_A and $total_cost_for_B hold the returned value from a mysql summation queries in the model. The queries return false if no record was found. Now, I'm trying to perform this operation.
if(($total_cost_for_A === FALSE) && ($total_cost_for_B === FALSE))
{
$data['no_record'] = 'No Record Found!';
$this->load->view('admin/bookings', $data);
}
else
{
$this->load->view('admin/summary', $data);
}
This test always fail and execute the else statement instead, which is not what. Any assistance will be appreciated. Thanks
Here are the functions
function total_cost_for_A($branch, $department, $date)
{
if($branch == 'Head Office' && $department == 'Summary')
{
$select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
WHERE bk.date = "'.$date.'"';
}
$result = $this->db->query($select_amount_paid);
if($result->num_rows() > 0)
{
return $result;
}
else
{
return FALSE;
}
}
function total_cost_for_B($branch, $department, $date)
{
if($branch == 'Head Office' && $department == 'Summary')
{
$total_LF_amount = 'SELECT SUM(total_amount) total_amount FROM large_format_print
WHERE date = "'.$date.'"';
}
$result = $this->db->query($total_LF_amount);
if($result->num_rows() > 0)
{
return $result;
}
else
{
return FALSE;
}
}
Try to change like this
$select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
WHERE date = '.$date;//i assume that date is an colum from graphics_booking
you are wrong with "bk.date" you can give directly if it is in your table with where condition and also try to avoid the same name for the function in the model and the varaible assigned.Try to change as different.otherwise your code looks good
No when u return u cannot return the entire , you need to return a row
so you need to do in your model as :
function total_cost_for_A($branch, $department, $date)
{
if($branch == 'Head Office' && $department == 'Summary')
{
$select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
WHERE bk.date = "'.$date.'"';
}
$result = $this->db->query($select_amount_paid);
if($result->num_rows() > 0)
{
return $result->row(); // you missed out here
}
else
{
return FALSE;
}
}
Same for the total_cost_for_B()