Last active
September 23, 2018 16:14
-
-
Save HenriqueSilverio/46ecf01377037b0fa539788580a4302e to your computer and use it in GitHub Desktop.
Laravel Roles and Capabilities seeder sample.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Models\Role; | |
use App\Models\Capability; | |
use Illuminate\Database\Seeder; | |
class PermissionsSeeder extends Seeder | |
{ | |
private $roles = [[ | |
'slug' => 'administrator', | |
'label' => 'Administrador', | |
'capabilities' => [[ | |
'slug' => 'manage-permissions', | |
'label' => 'Gerenciar Permissões', | |
],], | |
], [ | |
'slug' => 'moderator', | |
'label' => 'Moderador', | |
'capabilities' => [], | |
], [ | |
'slug' => 'manager', | |
'label' => 'Lojista', | |
'capabilities' => [], | |
], [ | |
'slug' => 'editor', | |
'label' => 'Gerente', | |
'capabilities' => [], | |
], [ | |
'slug' => 'customer', | |
'label' => 'Cliente', | |
'capabilities' => [], | |
],]; | |
public function run() | |
{ | |
foreach ($this->roles as $roleData) { | |
$role = Role::create([ | |
'slug' => $roleData['slug'], | |
'label' => $roleData['label'], | |
]); | |
foreach ($roleData['capabilities'] as $capabilityData) { | |
$capability = Capability::create([ | |
'slug' => $capabilityData['slug'], | |
'label' => $capabilityData['label'], | |
]); | |
$role->capabilities()->attach($capability->id); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment