Most of medium/big projects needs to users roles so we provide to you the easy way.
You can create multilingual role, you can use the way that you prefer.
1use Pharaonic\Laravel\Users\Models\Roles\Role; 2 3// Code (string) 4// Title (string or array) 5// Permissions (array of permissions codes) 6// Locale (string) (only with string title) 7 8// Way 1 9Role::set('admin', 'Administrator');10 11// Way 212Role::set('admin', 'Administrator', [], 'en');13 14// Way 315Role::set('admin', [16 'ar' => ['title' => 'إداري'],17 'en' => ['title' => 'Administrator']18], ['post.create', 'post.update', 'post.delete']);
Attach permission to the role.
1use Pharaonic\Laravel\Users\Models\Roles\Role;2 3$role = Role::findByCode('admin');4$role->permit('post.create', 'post.edit', 'post.delete');
This is how you can include the role into your user model.
1namespace App\Models;2 3use Pharaonic\Laravel\Users\Traits\HasRoles; 4 5class User extends Authenticatable6{7 use HasRoles; 8}
Giving the user one or many roles.
1$user->entrust('DevOps', 'Developer');2// Returns Boolean
Check if the user has one of these roles.
1$user->entrustedAny(['Developer', 'Musician']);2// Returns Boolean
Distrusting the user from having these roles.
1$user->distrust('Musician');2// Returns Boolean
Check if the user has not all these roles.
1$user->distrusted(['CEO', 'CFO']);2// Returns Boolean
Check if the user has not one of these roles.
1$user->distrustedAny(['CEO', 'CFO', 'Musician']);2// Returns Boolean
Clear all user roles and give him/her these list.
1$user->syncRoles('CEO', 'Developer', 'Musician');2// Returns Boolean
You can use middlewares to strict your routes.
1Route::middleware('entrusted:CFO')->group(...);2Route::middleware('entrustedAny:CFO,CEO')->group(...);