imnpc/nest-tool

一个 Laravel Nova 资源工具。

1.0.0 2023-08-22 07:32 UTC

This package is auto-updated.

Last update: 2024-09-22 09:49:03 UTC


README

此 Nova 包允许您在层级中嵌套项目。

image

安装

composer require aiman/nest-tool

示例迁移模式

class CreateTopicsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('topics', function (Blueprint $table) {
            $table->id();
            $table->text('topic');
            $table->text('slug')->nullable();
            $table->integer('order')->default(1);
            $table->integer('parent_id')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('topics');
    }
}

使用方法

为了使用 nest 工具,需要一个模型,该模型包含一个排序列、父列以及每个项目中显示的显示名称。示例见上方

use Aiman\ThaanaTextField\ThaanaTextField;

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            
            //other fields
            
            NestTool::make()
             ->usingModel(\App\Models\Topic::class)
             ->slug('slug')
             ->orderColumn('order')
             ->parentColumn('parent_id')
             ->displayName('topic')
             ->disable(false)
        ];
    }

关系

usingModel() 函数中输入的模型应具有此关系

function parent(){
    return $this->belongsTo('App\Models\Topic', 'parent_id');
}

function children(){
    return $this->hasMany(self::class, 'parent_id')->orderBy('order')->with('children');
}

模型

此函数需要 nest 工具使用的模型

NestTool::make()->usingModel(\App\Models\Topic::class)

别名

此函数需要要显示的别名字段。默认为 slug

NestTool::make()->slug('slug')

排序

此函数需要模型中用于排序的排序列名称。默认为 order

NestTool::make()->orderColumn('order')

父 ID

此函数需要模型中用于嵌套项目在父项目下的父列名称。默认为 parent_id

NestTool::make()->parentColumn('parent_id')

主题

此函数需要模型中用于显示在各个项目中的显示名称的列名称。默认为 topic

NestTool::make()->displayName('topic')

重要

此包已在 nova 2.0+ 上测试。最新测试版本为 nova v3.6.0