src/Resources/Role.php
<?php
namespace KodeKeep\NovaPermission\Resources;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use KodeKeep\NovaPermission\Fields\Permissions;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Nova;
use Laravel\Nova\Resource;
class Role extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model;
/**
* Indicates if the resource should be displayed in the sidebar.
*
* @var bool
*/
public static $displayInNavigation = false;
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = ['name'];
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function fields(Request $request)
{
$guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) {
return [$key => $key];
});
$userResource = Nova::resourceForModel(getModelForGuard($this->guard_name));
return [
Text::make('Name')
->rules(['required', 'string', 'max:255'])
->creationRules('unique:'.config('permission.table_names.roles'))
->updateRules('unique:'.config('permission.table_names.roles').',name,{{resourceId}}'),
Select::make('Guard Name')
->options($guardOptions->toArray())
->rules(['required', Rule::in($guardOptions)]),
Permissions::make('Permissions'),
MorphToMany::make($userResource::label(), 'users', $userResource)->searchable(),
];
}
/**
* Get a fresh instance of the model represented by the resource.
*
* @return mixed
*/
public static function newModel()
{
static::$model = config('permission.models.role');
$model = static::$model;
return new $model();
}
}