BelongsTo Field for Laravel Nova which allows to choose or create a resource
Ever had the following database structure and did not want the user to create the related resource (product_types) seperatly since it only consists of a unique name. But you don't want to put the product_type directly in the products table, since it would violate the third normal form.
This package solves this, by keeping the original belongsTo field but allowing the user to create a new resource by simply entering a not yet existing name. Additionally it includes the prepopulate-searchable package from alexbowers.
composer require sparclex/nova-creatable-belongs-to
Add field inside your fields Array. The parameters consist of the same ones as the laravel nova belongsTo field with an additional nameAttribute
which determines the name of the display attribute in the related model (default: name).
public function fields(Request $request)
{
return [
ID::make()->sortable(),
CreatableBelongsTo::make('ProductType', 'productType', 'App\Nova\ProductType', 'name')
->prepopulate()
];
}
- Tests
- Respect policies for creation
The MIT License (MIT). Please see License File for more information.