Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Twoopa's avatar

Different dashboards for different people

I'm building an app that requires to have 2 different dashboards. But I'm trying to not make it where it's "dashboardname1" and "dashboardname2". I want just "dashboard" for both types of users. I've thought of a couple of way to do this but they both seem like more work than it should be.

I'd like to know from the best community to ask this question what y'all think of the best way to do this. If you would like more information please ask. I'm sorry if it isn't enough cause I'm wanting to keep my project under till it's time. Which obviously I have a long way to go ? Thanks everyone

0 likes
12 replies
Snapey's avatar

So you must have one route

Route::get('dashboard', 'DashboardController@index');

Dashboard Controller can make the call about what type of user they are and show the appropriate view file.

eg;

    if($request->user->IsVendor){
        return view('vendor.dashboard');
    }

    return view('client.dashboard');

Twoopa's avatar

I see what you are saying there! Thank you.

So after this, how would i separate out the controllers for the different dashboards. Since we have a route that you showed, how could I differentiate that which dashboard belongs to who to make sure the correct controllers are being used.

Ugh, I think I just confused myself trying to explain that.

Snapey's avatar
Snapey
Best Answer
Level 122

The controllers will come from the routes that are embedded in the dashboard itself, i.e., all the links on one dashboard will go to just one set of controllers.

Regarding display of data on the dashboards themselves, it might be an idea to delegate to another controller;


    if($request->user->IsVendor){
        return (new VendorDashboardController())->index($request);
    }

        return (new ClientDashboardController())->index($request);

6 likes
Twoopa's avatar

That's amazing. Thank you so much! I'm assuming that those if statements would go in the route function?

jlrdw's avatar

On one of my sites I simply have index.php, for users, and for admins I have indexadmin.php. I use the same controller but different methods for an admin vs a regular user. And simply have a separate top nav (admin area as some may call it) for admins.

Just work this stuff out in your controller and views.

And this stuff is just crud, css, one doesn't need a special admin package unless they choose to have one.

Snapey's avatar

I'm assuming that those if statements would go in the route function?

No, I was thinking in your dashboard controller. So instead of the first option I suggested returning one of two views, here I am suggesting letting the other controllers return the view that they each want, together with their own data.

A common /dashboard route is recieved by the Dashboard controller and then delegated to the other controllers according to the type of user. The data returned by those controllers is returned directly.

You could do it in a web.php closure, but this would stop you from caching your routes.

2 likes
Twoopa's avatar

Sorry for the late reply @Snapey

I got it figured out how you explained. Except now I am running into another issue.

Call to a member function hasRole() on null

Usning Laratrust, and this is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class DashboardController extends Controller
{
    public function index(Request $request, User $user)
    {
        if($request->user->hasRole('landlord')){
            return (new Landlord\LandlordDashboardController())->index($request);
        } 
        return (new Tenant\TenantDashboardController())->index($request);
    }
}

User is logged in, but it still cannot detect that it has a role, even though it does. I do have the Laratrust trait in the User Model, so it should be working.

verism's avatar

I think maybe that ought to be:

if($request->user()->hasRole('landlord'))

with parentheses. But I could be wrong.

2 likes
Twoopa's avatar

with parentheses. But I could be wrong.

Nope you were right. Thank you very much!

Snapey's avatar

Or you could use the $user that you've asked for in the controller parameters ;-)

Twoopa's avatar

I just gave that a shot, but when i changed the role from Tenant to landlord, it wouldn't redirect back to landlord. It would keep going to tenant.

I cleared cache, and dump-autoload.

Please or to participate in this conversation.