michielkempen/nova-polymorphic-field 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

michielkempen/nova-polymorphic-field

最新稳定版本:1.0.10

Composer 安装命令:

composer require michielkempen/nova-polymorphic-field

包简介

A Laravel Nova field container allowing to depend on other fields values

README 文档

README

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.

统计信息

  • 总下载量: 79.82k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 69
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 67
  • Watchers: 4
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固