PHP code example of acolorstory / nova-polymorphic-field

1. Go to this page and download the library: Download acolorstory/nova-polymorphic-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

acolorstory / nova-polymorphic-field example snippets


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');
});

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'),

                ]),

        ];
    }
}

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(),
            ...
        ];

use Illuminate\Database\Eloquent\Relations\Relation;

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