Laravel Multi Authentication
Installation video : click here or click below image
Before Laravel version 6.0
php artisan make:auth
From Laravel Version 6.0
1. composer require laravel/ui
2. php artisan ui vue --auth
3. npm install
4. npm run dev
composer require codexshaper/laravel-permission
php artisan permission:install
- Publish Configs
php artisan vendor:publish --tag=permission.config
- Publish Seeds
php artisan vendor:publish --tag=permission.seeds
- Migrate Database
php artisan migrate
- Run composer dump autoload
composer dump-autoload
- Seeding Database
php artisan db:seed --class=PermissionDatabaseSeeder
- Add Routes
Route::group(['prefix' => config('permission.prefix'),'middleware'=>['role:admin']], function () {
Permission::routes();
});
Import use CodexShaper\Permission\Traits\HasRoles
or simply use HasRoles
Trait into your App\User
Model
namespace App;
use CodexShaper\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
}
php artisan permission:install:demo
php artisan permission:publish:views
php artisan permission:publish:resources
'resources_path' => 'resources/views/vendor/permissions/assets',
'views' => 'resources/views/vendor/permissions/views',
use CodexShaper\Permission\Models\Permission;
$permission = Permission::create([
'name' => 'Browse',
'slug' => slug('browse'),
'created_at' => now(),
'updated_at' => now(),
]);
// Create Role before set permission
// $roles = [role_slug_or_id] ex: ['admin',1,2,'author']
$permission->givePermissionToRoles( $roles );
$role_ids = [1,3,5]
$permission->syncPermissionToRoles( $role_ids );
// Delete specific Roles
$role_ids = [1,3,5];
$permission->revokePermissionsFromRoles( $role_ids );
// Delete all roles for current permission
$permission->revokePermissionsFromRoles();
use CodexShaper\Permission\Models\Role;
$admin = Role::create([
'name' => 'Super Admin',
'slug' => 'admin',
'created_at' => now(),
'updated_at' => now(),
]);
$admin->assignPermissions([
'browse',
'read',
'edit',
'add',
'delete'
]);
$permission_ids = [1,3,5]
$admin->syncPermissions( $permission_ids );
// Delete specific Permissions
$permission_ids = [1,3,5];
$admin->revokePermissions( $permission_ids );
// Delete all roles for current roles
$admin->revokePermissions();
$admin->hasPermission( $permission_slug );
use App\User;
$user = new User;
$user->name = 'John Doe';
$user->email = 'john@gmail.com';
$user->password = Hash::make('password');
$user->save();
$user->assignRoles('admin');
$user = User::find(1);
$user->assignRoles('admin');
$user = User::find(1);
// Use pipe(|)
$user->assignRoles('admin|client|customer');
// Or use comma(,)
$user->assignRoles('admin,client,customer');
// Or use space
$user->assignRoles('admin client customer');
// Or Mixed
$user->assignRoles('admin client,customer|write');
// Pass custom separators
$separators = ',.| ';
$user->assignRoles('admin client,customer|write', $separators);
$role_ids = [1,2,3];
$user->syncRoles( $role_ids );
// Delete specific Roles for current User
$role_ids = [1,3,5];
$user->revokeRoles( $role_ids );
// Delete all roles for current user
$user->revokeRoles();
$user->hasRole( $role_slug );
Route::group(['middleware'=>['role:admin']],function(){
// Routes
});
@can('browse')
<p>You Can Browse</p>
@endcan
@role('admin')
<p>You are admin</p>
@endrole
@hasrole('admin')
<p>You have admin Permission</p>
@endhasrole
@haspermission('edit')
<p>You have admin Permission</p>
@endhaspermission
See also the list of contributors who participated in this project.
- MIT license
- Copyright 2019 © CodexShaper.