定制 michielkempen/nova-polymorphic-field 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

michielkempen/nova-polymorphic-field

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.

michielkempen/nova-polymorphic-field 适用场景与选型建议

michielkempen/nova-polymorphic-field 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 82.9k 次下载、GitHub Stars 达 67, 最近一次更新时间为 2018 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nova」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 michielkempen/nova-polymorphic-field 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 michielkempen/nova-polymorphic-field 我们能提供哪些服务?
定制开发 / 二次开发

基于 michielkempen/nova-polymorphic-field 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-17