Skip to content
Advertisement

How to redirect a user to different dashboards based on a level field in the ”’users”’ table in Laravel Breeze?

Please forgive me if my question is too generic but I am at my wits end.

I have a users table with a level field as shown given below:

$table->unsignedSmallInteger('level')->after('password');

I want to redirect the user to different dashboards based on the value of the level field. For example for level 0 user

admindashboard

for level 1 user

userdashboard

I am using Laravel 8.4 and laravel/breeze for authentication.

Thank You

Advertisement

Answer

Ok, found a solution.

  1. On routesweb.php I have commented out the default dashboard route and created a new route for /dashboard and pointed it to AppHttpControllersDashboardsController@index
// Route::get('/dashboard', function () {
//     return view('dashboard');
// })->middleware(['auth'])->name('dashboard');

require __DIR__.'/auth.php';

Route::get('/dashboard','AppHttpControllersDashboardsController@index')->middleware(['auth'])->name('dashboard');
  1. index function of DashboardsController
public function index() {
        $data['authenticated_user'] = Auth::user();
        if($data['authenticated_user']->level == 0) {
            echo "Admin";
        } elseif($data['authenticated_user']->level == 1) {
            echo "Employee";
        } else {
            echo "Security";
        }
    }

Basically this will accomplish what I want to achieve.

Thanks

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement