forked from Advisr/nova-permission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttachToRole.php
43 lines (38 loc) · 1.04 KB
/
AttachToRole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Vyuldashev\NovaPermission;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Select;
class AttachToRole extends Action
{
use InteractsWithQueue, Queueable, SerializesModels;
/**
* Perform the action on the given models.
*
* @param ActionFields $fields
* @param Collection $models
* @return mixed
*/
public function handle(ActionFields $fields, Collection $models)
{
$role = Role::getModel()->find($fields['role']);
foreach ($models as $model) {
$role->givePermissionTo($model);
}
}
/**
* Get the fields available on the action.
*
* @return array
*/
public function fields()
{
return [
Select::make('Role')->options(Role::getModel()->get()->pluck('name', 'id')->toArray())->displayUsingLabels(),
];
}
}