Giter Club home page Giter Club logo

nova-polymorphic-field's Introduction

Nova Polymorphic Field

Latest Version on Packagist Total Downloads License

Description

A Laravel Nova field that allows you to create a collection of polymorphic resources.

Depending on the polymorphic type you select:

  1. Different fields will be populated on the form/detail page of your resource.
  2. Records will be automatically created/updated in the corresponding tables.

Scheme

Demo

Scheme

Installation

The package can be installed through Composer.

composer require michielkempen/nova-polymorphic-field

Usage

  1. Add a morphs field to the migration of your base model.
  2. Add the MichielKempen\NovaPolymorphicField\HasPolymorphicFields trait to your Nova Resource.
  3. Add the MichielKempen\NovaPolymorphicField\PolymorphicField to your Nova Resource fields method.
  4. Specify the different polymorphic types by calling the type($name, $modelClass) method on the PolymorphicField.
    • The $name parameter is a readable name you assign to your polymorphic type.
    • The $modelClass parameter is the class of the polymorphic model.

Example

Migrations:

Schema::create('news_posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->morphs('type'); // !!
    $table->timestamps();
});

Schema::create('videos', function (Blueprint $table) {
    $table->increments('id');
    $table->string('url');
});

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('image');
    $table->text('text');
});

Resource:

class NewsPost extends Resource
{
    use HasPolymorphicFields;

    public function fields(Request $request)
    {
        return [
            
            Text::make('Title'),

            PolymorphicField::make('Type')
                ->type('Video', \App\Video::class, [

                    Text::make('Url'),

                ])
                ->type('Article', \App\Article::class, [

                    Image::make('Image'),

                    Textarea::make('Text'),

                ]),

        ];
    }
}

You can optionally hide the type selection when updating a resource. This can be useful if you don't want the user to be able to change the Type of a polymorphic relationship once it has been created.

class NewsPost extends Resource
{
    use HasPolymorphicFields;

    public function fields(Request $request)
    {
        return [
            ...
            PolymorphicField::make('Type')
                ->type('Video', \App\Video::class, [
                    Text::make('Url'),
                ])
                ->type('Article', \App\Article::class, [
                    Image::make('Image'),
                    Textarea::make('Text'),
                ])
                ->hideTypeWhenUpdating(),
            ...
        ];

morphMap

By default, the fully qualified class name of the related model will be stored as type field in the base model. However, you may wish to decouple your database from your application's internal structure. In that case, you may define a relationship "morph map" to instruct Eloquent to use a custom name for each model instead of the class name:

use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'article' => \App\Article::class,
    'video' => \App\Video::class,
]);

You may register the morphMap in the boot function of your AppServiceProvider.

License

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

nova-polymorphic-field's People

Contributors

jasonlav avatar m2de avatar mbardelmeijer avatar michielkempen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.