Skip to content

BelongsTo Field which allows to choose or create a resource

License

Notifications You must be signed in to change notification settings

sportsrecruits/nova-creatable-belongs-to

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

TODO

  • Tests
  • Respect policies for creation

License

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

About

BelongsTo Field which allows to choose or create a resource

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Vue 67.1%
  • PHP 29.9%
  • JavaScript 3.0%