Home > Software engineering >  Laravel more than one role
Laravel more than one role

Time:08-06

I'm trying to make it possible to have 2 roles and switchable.

(PHP v7.2.5 with laravel v7.30.6)

I got a server-side API and admin panel on laravel/nova, which is currently in production. I have a users table(MySQL) and I have a role field that contains one string('Admin' or 'Client' etc) it's possible to be only one role at a time.

user model This is how I check the user's role.

I tried using the Spatie roles package, but my problem is that I need to move all my production users to the new table created by Spatie, which is inefficient.

is there a good solution you know that will make it easier and faster to implement?

CodePudding user response:

You can create a middleware to check for example if user is admin or not.

Path: app/Http/Middleware/EnsureAdminIsValid.php

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class EnsureAdminIsValid
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::user() && Auth::user()->is_admin == 1) {
             return $next($request);
        }

        return redirect('/normal-role-users-page')->with('error','You are not Admin');
    }
}

is_admin is a table in USER table. If you already have a table change name in middleware and value can be string or boolean (true, false).

In app/Http/Kernel.php add:

protected $routeMiddleware = [        
        'admin' => \App\Http\Middleware\EnsureAdminIsValid::class,

Now you have a web middleware and you can add it to any URL or construct function of any controller.

route: // Admin dashboard

Route::group(['middleware' => ['auth', 'admin']], function () {
    Route::get('/admin-dashboard', 'Admin\AdminController@index')->name('admin.dashboard');
});

controller:

 public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('admin'); 
    }

CodePudding user response:

If I undestand correctly, you want your user to have multiple roles instead of only one.

You must change create a role table with a pivot table (i.e role_users) then add a belongsToMany relationship in your user model.

public function roles()
{
    return $this->belongsToMany(Role::class);
}

You will then have to modify the role verification logic to something like this.

public function isAdmin()
{
    return $this->roles()->where('name', 'admin')->exists();
}

Another solution which requires less migration effort but is less scalable (i.e. if you add roles, you will need to add more and more fields to the users table): Split the "role" column to two boolean columns is_admin and is_business.

You can then modify your isAdmin and isBusiness methods to

public function isAdmin()
{
    return $this->is_admin === 1;
}

public function isBusiness()
{
    return $this->is_business === 1;
}
  • Related