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

mallorca's avatar

Show different views based on user role

I'm new to Laravel and I'm using v5.1 in my project. I have created most of the business logic and it works great (I'm starting to really love Laravel!), but I could really need some guidance from a experienced Artisan to re-architecture it. I appreciate all the guidance and tips I can get!

Right now I have 4 tables, one for 'users', one for 'personal articles', one for 'club articles' and one for 'clubs'. It has 3 different roles, 'user', 'coach' and 'admin.

'users' can create their own articles and edit their personal info. 'Coaches' can create 'club articles’ that everyone who belongs to the same club can see, they can also create ’users’ but only within their own club. Admins have full permissions.

I have 4 controllers, one AdminsController, CoachesController, ArticlesController and ClubArticlesController where all of them have their own model. Each of these have their own route::resource and views right now using their own permalink, however this current structure is not useful for me.



I would like to have one route for the front-end where articles are going to be displayed for the user, and one view for the back-end. Depending on what role a user has, different sections of the back-end should be shown.

What would be a good practice to structure the routes/controllers to display different admin views depending on roles (I have already the roles defined)? Should I have something like '/frontend’ and ’/backend’ with route::resource, and add conditional statements to the controllers public functions? Or maybe have several routes with conditional statements to it? What would be a good way to accomplish this?

0 likes
9 replies
willvincent's avatar

Do the views need to be different, or just the data passed to those views? Seems to me it would just be the data passed to the view that varies

mallorca's avatar

@willvincent thanks for your reply! Could you please elaborate what you mean? Users should have access to different functions in the back-end view depending on their role.

Do you perhaps mean that I should add conditional logic in the view itself (blade file)? I'm new to laravel so I would appreciate a detailed explanation, perhaps with code examples. Thanks!

mallorca's avatar

@mstnorris Thanks for yor reply! I'm already using middleware for authentication and I have already created user the user roles.

What I'm wondering is how to structure the routes/controllers/views for my case (described above), what would be a good practice?

helmerdavila's avatar

@bobeta I'm following the next structure

// routes.php
Route::group(['prefix' => 'admin', 'as' => 'admin::', 'middleware' => ['admin']], function(){
    get('foo/bar', .....);
});
Route::group(['prefix' => 'user', 'as' => 'user::', 'middleware' => ['user']], function(){
    get('foo/bar', .....);
});
// controllers
app/Http/Controllers/Admin/TestController.php
app/Http/Controllers/User/TestController.php
// views structure
views/admin/foo/view.blade.php
views/user/foo/view.blade.php
uxweb's avatar
uxweb
Best Answer
Level 20

@bobeta You can do that in your view, for example:

// within in some view

@if(Auth::user->hasRole('admin'))
   // show content related to it
@endif 

And for that hasRole method, you can create it within your User model:

class User
{
    /**
     * Check if this user belongs to a role
     *
     * @return bool
     */
    public function hasRole($role)
    {
        return $this->role == $role;
    }
}
3 likes
commander_newbie's avatar

Even if the question and the requester is far away by now, the solution has a little typo. Not sure if this has changed to be invalid over time.

This does not work for me:

@if(Auth::user->hasRole('admin'))

This however does:

@if(Auth::user()->hasRole('admin'))
2 likes
emco83's avatar

Yes, that's right. Only this works.

@if(Auth::user()->hasRole('admin'))

Please or to participate in this conversation.