vincent15000's avatar

A different dashboard for each role

Hello,

In the application, users have a unique role.

The roles are : superadmin, admin, member, user.

Each role has a different dashboard.

Here are the different solutions I have thought about.

  • have a unique dashboard route and a unique dashboard view (dashboard.blade.php), but load inside this view different partial views according to the role @include('dashboard.'.$role) to load for example dashboard/_admin.blade.php
views
|-- dashboard
|--|-- dashboard.blade.php
|--|-- _admin.blade.php
|--|-- _member.blade.php
...
  • have one different dashboard route, dashboard controller and dashboard view for each role (for example admin.dashboard, admin/dashboard.blade.php and Admin/AdminDashboardController)
views
|-- admin
|--|-- dashboard.blade.php
|-- member
|--|-- dasboard.blade.php
...

What is the best way ?

Thanks for your help.

V
0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

Perhaps, a single /dashboard endpoint served by an invokable Controller with protected/private methods for each role, e.g.

Route::get('dashboard', DashboardController::class)
    ->middleware('auth')
    ->name('dashboard');
class DashboardController
{
    public function __invoke(Request $request)
    {
        $role = auth()->user()->role ?? 'user';
        abort_unless(method_exists($this, $role), 400);
        return  $this->{$role}();
    }

    private function superadmin()
    {
        // organize data for SuperAdmin dashboard
       return view('dashboards.superadmin', ['data' => $data]);
    }

    private function admin()
    {
        // organize data for Admin dashboard
       return view('dashboards.admin', ['data' => $data]);
    }
    // ... etc
}

The views directory structure would look like this:

views
|-- dashboards
|--|-- superadmin.blade.php
|--|-- admin.blade.php
|--|-- member.blade.php
|--|-- user.blade.php

I think I would find this approach easily discoverable from the point-of-view of the developer. Furthermore, any user is just seeing a /dashboard URI which doesn't indicate anything about other dashboards for the other roles

2 likes
vincent15000's avatar

@tykus That's exactly what I thought about ... any user is just seeing a /dashboard URI and nothing let think that there are other dashbaords.

And your suggestion about how to write the dashboard controller is pretty fine.

krisi_gjika's avatar

I would suggest the same controller and different views. Than when you notice some blade code duplicates, example they all have a profile section, or both superadmin and admin have a user management section, you can create partials/component out of those.

1 like

Please or to participate in this conversation.