BelongsTo Field which allows to choose or create a resource
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
resources/js
src
.gitignore
LICENSE
README.md
composer.json
package.json
webpack.mix.js

README.md

Creatable BelongsTo Field for Laravel Nova

BelongsTo Field for Laravel Nova which allows to choose or create a resource

Latest Stable Version Total Downloads Latest Unstable Version License StyleCI

Use Case

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.

Database setup example

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.

Installation

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\ProductType', 'name')
            ->prepopulate()
    ];
}

TODO

  • Tests
  • Respect policies for creation

License

The MIT License (MIT). Please see License File for more information.