/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $permissions = ['C' => 'Create', 'R' => 'Read', 'U' => 'Update', 'D' => 'Delete'];
     $requests = str_split(strtoupper($this->option('only')));
     foreach ($requests as $permissionTo) {
         $permission = new Permission();
         $permission->name = $this->argument('name') . '.' . strtolower($permissions[$permissionTo]);
         $permission->label = $permissions[strtoupper($permissionTo)] . ' ' . $this->argument('name');
         $permission->save();
         if (!is_null($this->option('assign'))) {
             if (is_numeric($this->option('assign'))) {
                 try {
                     /** @var Role $role */
                     $role = Role::findOrFail($this->option('assign'));
                     $role->givePermission($permission);
                 } catch (QueryException $e) {
                     $this->error('Role not found!');
                 }
             } else {
                 $this->error('The supplied role id is not a number!');
             }
         }
     }
 }
Beispiel #2
0
 /**
  * @return \Illuminate\Database\Eloquent\Collection|static[]
  */
 protected function getPermissions()
 {
     return Permission::with('roles.permissions')->get();
 }