laravel - Dynamic route group prefix as role

576 Views Asked by At

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

//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 am passing a static department id in helper, But if I tried to get logged in user department I am getting the following error


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

0

There are 0 best solutions below