Dec 21, 2021
0
Level 3
Nova - Dynamic set inline relation field
I have one table for categories where the category could be for several different models, e.g. Employee, Client etc and the model is defined by a field called "type". In the type field I will store the model class, e.g. Employee::class. This way I can retrieve and display only the categories for Employees, the category table schema is;
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->integer('company_id');
$table->string('type');
$table->string('name');
$table->timestamps();
});
Is it possible to default the type field to Employee when using the showCreateRelationButton method from inside the Nova Employee resource?
Employee Resource;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
Text::make(__('ID'), 'ident')->sortable(),
Boolean::make(__('Status'))
->default(1),
BelongsTo::make(__('Category'))
->showCreateRelationButton() // SET DEFAULT HERE?
->nullable(),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Text::make('Email')
->sortable()
->rules('required_if:status,1', 'nullable', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Text::make(__('Address')),
Text::make(__('Phone')),
Color::make('Calendar Colour')
->onlyOnForms()
->showOnDetail(),
];
}
Category Resource;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make(__('ID')),
Text::make(__('Name')),
Select::make(__('Type'))->options([
'Employee::class' => 'Employee',
'Client::class' => 'Client',
]),
];
}
Please or to participate in this conversation.