1

4 weeks agoby Ajay Kumar

Route group prefix as per user type

Hello all, I have a problem I want to use user type in laravel route group prefix, after there login. But I am not getting a perfect way to do that. eg: I have three user type- 1- admin 2- executive 3- user

I want to make a dynamic route prefix
eg:
		1- admin/dashboard
		2- executive/dashboard
		3- user/dashboard
		
I am trying to do this with helper file ( Auto load )

//Getting department name for url prefix  ---
----------------------------------------------
helper.php
----------------------------------------------

function routePrefix(){ $userDepartment = ''; if(Auth::user() != ''){ $dept_id = Auth::user()->department_id; $userDepartment = Str::of(Department::where('id', $dept_id)->first()->department)->slug('-'); } return $userDepartment; }

Here i am unable to get auth details in helper file, cause of auth middleware in my route group


web.php +++++++++++++++++++++++++++++++++

Route::group([ 'middleware' => 'auth', 'accessControl', 'prefix' => routePrefix(), ], function () {

    Route::get('dashboard', 'UserController@index')->name('user.index')

})

Here every thing is working correctly when i passing a statict department id in helper But if I tried to get logged in user department I am getting an error

ErrorException Trying to get property 'department_id' of non-object

2

Bhagyaraj Aj replied 1 week ago

0

This seems to be,the user you logged in doesn't have department_id property.First put dd() on the helper function to check that the logged in user has the property

Ask Question
Most Visited
Categories