You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I get this error when I look to attach a role or permission. Can anyone help?
<?php
namespace App\Nova;
use App\Models\Country;
use Laravel\Nova\Panel;
use Illuminate\Support\Str;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\Text;
use App\Nova\Metrics\NewUsers;
use Laravel\Nova\Fields\Select;
use Illuminate\Validation\Rules;
use Laravel\Nova\Fields\HasMany;
use App\Nova\Metrics\UsersPerDay;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Actions\Actionable;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Http\Requests\NovaRequest;
use YieldStudio\NovaPhoneField\PhoneNumber;
class User extends Resource
{
use Actionable;
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = \App\Models\User::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'name', 'email',
];
/**
* The used for hasOne related fields.
*
*/
protected static function fillFields(NovaRequest $request, $model, $fields)
{
// Ticker for result array
$count = 0;
// The prefix of relatable columns within fields.
// User::first()->profile->first_name
// Text::make('Voornaam', 'profile.first_name');
// $request->input('profile_first_name');
$filters = ['profile_'];
// Loop through all defined filters above
foreach ($filters as $filter) {
// Setup relations
$relations[Str::replaceFirst('_', '', $filter)] = self::rejectAndFlattenRelation($filter, $request);
}
// Fill all primary fields after relations have been extracted from the request
$result = parent::fillFields($request, $model, $fields);
// Some complex iteration through all extracted relation requests
foreach ($relations as $method => $relation) {
// Append them to the final result accordingly
$result[++$count][] = function () use ($relation, $method, $model) {
// Create if not exists otherwise update with relation fields
$model->{$method}()->updateOrCreate([], $relation);
};
}
// Finally return result
return $result;
}
private static function rejectAndFlattenRelation($filter, &$request)
{
// Grab all the currently available inputs
$input = collect($request->all());
// Grab all relatable fields according to filter
$relatableFields = $input->reject(function ($item, $key) use ($filter) {
return strpos($key, $filter) === false;
});
// Remove relation ship fields from request
$request->except($relatableFields->toArray());
// Finally flatten collection and return as array
return $relatableFields->flatMap(function ($item, $key) use ($filter) {
return [Str::replaceFirst($filter, '', $key) => $item];
})->toArray();
}
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
new Panel('User Details: '.$this->name , $this->registrationFields()),
new Panel('Personal Information', $this->personalFields()),
new Panel('Contact Information', $this->contactFields()),
HasMany::make("User Employers"),
HasMany::make('User Notes'),
MorphToMany::make('Roles', 'roles', \Vyuldashev\NovaPermission\Role::class),
MorphToMany::make('Permissions', 'permissions', \Vyuldashev\NovaPermission\Permission::class),
];
}
/**
* Get the registration fields for the resource.
*
* @return array
*/
protected function registrationFields()
{
return [
ID::make()->sortable(),
Gravatar::make()->maxWidth(50),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Text::make('Email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Password::make('Password')
->onlyOnForms()
->creationRules('required', Rules\Password::defaults())
->updateRules('nullable', Rules\Password::defaults()),
];
}
/**
* Get the personal fields for the resource.
*
* @return array
*/
protected function personalFields()
{
return [
Date::make('Date of Birth', 'profile.date_of_birth')
->hideFromIndex()
->nullable(),
Select::make('Sex', 'profile.sex')
->options([
'1' => 'Male',
'0' => 'Female',
])
->displayUsingLabels()
->hideFromIndex(),
Select::make('Marital Status', 'profile.marital_status')
->options([
'1' => 'Single',
'2' => 'Married',
'3' => 'Widowed',
'4' => 'Divorced',
'5' => 'Separated',
])
->displayUsingLabels()
->hideFromIndex(),
];
}
/**
* Get the contact fields for the resource.
*
* @return array
*/
protected function contactFields()
{
return [
PhoneNumber::make('Phone', 'profile.phone')
->hideFromIndex(),
Select::make('Country', 'profile.country_id')
->displayUsingLabels()
->hideFromIndex()
->searchable()
->options(function () {
return array_filter(Country::pluck('name', 'id')->toArray());
}),
Text::make('State / Province / Region', 'profile.state_id')
->hideFromIndex(),
Text::make('Address', 'profile.address')->hideFromIndex(),
Text::make('City', 'profile.city')->hideFromIndex(),
Text::make('Zip Code', 'profile.zip')->hideFromIndex(),
];
}
/**
* Get the cards available for the request.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function cards(NovaRequest $request)
{
return [
NewUsers::make(),
UsersPerDay::make()
];
}
/**
* Get the filters available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function filters(NovaRequest $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function lenses(NovaRequest $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function actions(NovaRequest $request)
{
return [];
}
}
The text was updated successfully, but these errors were encountered:
I get this error when I look to attach a role or permission. Can anyone help?
The text was updated successfully, but these errors were encountered: