aiman / nest-tool
Laravel Nova 资源工具。
v1.0.0
2020-06-18 08:11 UTC
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2024-09-29 05:55:40 UTC
README
这个 Nova 包允许你在层次级别中嵌套项目。
安装
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');
}
}
用法
为了使用嵌套工具,它需要一个模型来使用,该模型包括一个顺序列、父列和每个项目显示的显示名称。示例见上方
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');
}
模型
此函数需要嵌套工具使用的模型
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