I have one route that switches the routes based on the permission the user has. Here is a little example:

public function show(Company $company)
    if(auth()->user()->hasPermission('read-companies')) {
        return view('portal.company.showAdmin', compact('company'));
    } elseif (auth()->user()->hasPermission('read-company') && auth()->user()->companies->contains($company)) {
        return view('portal.company.showEntrepreneur', compact('company'));
    } elseif(auth()->user()->hasPermission('read-companies_v') && $company->visible()) {
        return view('portal.company.showTrainee', compact('company'));
    } else {

This basicly returns to 3 views based on the permissions of the user. A lot of my controller functions look like this. Now my question: Is this "One controller/route for multiple actions" approach a bad Idea? What if I want to add a role to the auth()->user()->companies->contains($company) many to many relationship? I would need to add all my controller functions. Is it better to have a function on the user model like IsAbleToShowOnCompany($company)? But than I would have houndreads of functions like that, because, e.g., Companys can create pokes, and I would need to check if the role of the many to many relation to company would allow that.

Do you have any refactor Ideas?

I've heard about service and repository classes, but how can I refactor this into a servivce?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.