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

clusteCode's avatar

One controller for different roles and query

Hello to all,

I am new in laravel and I still have many doubts. I present you my problem.

I'm building an app for the management of condominiums, the peculiarity and that each user will have different queries and if necessary different permissions (policy).

I'll give you a small example for what I mean by query + different views for each user:

the 'condominium' can read the table / model 'Bill' where it displays 'Data' and 'Cost' to be paid.   the 'chief condominium' ( Always on the same table ) can have a broader vision, reading other data, perhaps more technical and if necessary make a JOIN with related tables.

In this case, what changes is the query that generates the results of the view.

How can I manage different queries based on different users roles ?

I tried some solutions but ...

-- FIRST SOLUTION: it was to create a single reference to the resource:

site.com/bill

where thanks to the management of permissions and roles defined in the database I could display, modify, delete etc ....

( Click here for SCHEMA DB ER: https://drive.google.com/file/d/1wWwLxc4qW2Cy97TaDN9IosKaHG9-Hs0z/view?usp=sharing )

but I can filter your user roles only through an IF. This does not make the code clean if for example I will add 10 roles :-(

-- SECOND SOLUTION:

To solve the problem of query + dedicated views for each user I have defined different routes for different controllers, like this:

site.com/condominiums/bill

site.com/chief-condominium/bill

site.com/admin/bill

site.com/.../bill

Now I can generate different queries + view but code maintenance is laborious.

What do you recommend?

Thank you so much for the support! Andrea

0 likes
5 replies
mballaag's avatar

you can use Laravel Spatie. This package allows you to manage user permissions and roles in a database. it's simple and very fast ti implement. it's also allow you to manage permission on routes, controllers and blade.

clusteCode's avatar

@mballaag Thanks for the immediate reply :-). Can I also manage different queries based on the role?

martinbean's avatar
Level 80

@clusteCode If you have a “CondominiumController” that fetches a condominium and displays different data depending on the role of the user logged in, then I think you want to have a way of switching the view rather than performing different queries.

You could use Laravel’s “Responsable” interface and add it to a view model that picks a view depending on the authenticated user’s role:

namespace App\Http\Controllers;

use App\Condominium;
use App\Http\Views\CondominiumView;

class CondominiumController extends Controller
{
    public function show(Condominium $condominium)
    {
        return new CondominiumView($condominium);
    }
}
namespace App\Http\Views;

use Illuminate\Contracts\Support\Responsable;
use Exception;

class CondominiumView implements Responsable
{
    private $condominium;

    public function __construct(Condominium $condominium)
    {
        $this->condominium = $condominium;
    }

    public function toResponse($request)
    {
        // If your User model has a property called role…
        switch ($request->user()->role) {
            case 'normal_user':
                $path = 'condominium.show.normal_user';
                break;

            case 'admin':
                $path = 'condominium.show.admin';
                break;

            default:
                throw new Exception("No view for role [{$role}].");
        }

        return view($path, [
            'condominium' => $this->condominium,
        ]);
    }
}

So a user can view a single condominium, but the view they get to see depends on their role.

1 like
clusteCode's avatar

Hello @martinbean, I'm surprised by the immediate response and receive good code :-) Thank you very much.

I did not know 'Responsable' in laravel, I try it now, surely this makes the code cleaner :-P

Unfortunately, in this application every role will have a different query :-(, for you, the only way to solve this problem and insert the IFs inside the controller?

Thanks again for your time and availability.

Please or to participate in this conversation.