parfumix/nova-creatable-belongs-to

BelongsTo field for laravel nova which allows to choose or create a resource

1.0.3 2019-03-19 15:50 UTC

This package is auto-updated.

Last update: 2024-10-19 21:54:21 UTC


README

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

TODO

  • Tests
  • Respect policies for creation

License

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