我正在使用OctoberCMS 、Apache 和 PHP7。
我使用生成器创建了一个自定义插件。
我可以添加记录,一切正常。除非我按重新排序记录
按钮,否则它会显示空记录列表。
有关于如何设置的指南吗?
我正在看:
https://octobercms.com/docs/database/traits#nested-tree
https://octobercms.com/docs/api/october/rain/database/traits/nestedtree
我添加到模型:
使用\October\Rain\Database\Traits\NestedTree;
以及数据库列:
parent_id
、nest_left
、nest_right
、nest_深度
。
我应该在哪里放置$table->integer('parent_id')->nullable();
?
如果我将其放入模型中,则会出现错误解析错误:语法错误,意外的“$table”(T_VARIABLE),期望函数(T_FUNCTION)
。
记录
重新排序记录:空列表
最佳答案
首先,您不需要使用nestable
,因为我们不处理树
,而且似乎有一个指南,但很简短。
我们可以对 \October\Rain\Database\Traits\Sortable
进行排序感到满意,因为我们不需要 tree
我们可以跳过添加这些
use \October\Rain\Database\Traits\NestedTree;
parent_id, nest_left, nest_right, nest_depth
如果我们使用trait
,则需要一个特定的列名称sort_order
,但是如果需要,我们可以通过定义const SORT_ORDER = 'my_sort_order';来更改它
code> 在我们的模型
中。
由于您已经构建了表,因此您可以使用构建器插件更新表定义
并将sort_order
字段添加到表中。
or manually you can use this script and add it to
version.yaml
file [ plugins\hardiksatasiya\demotest\updates ( respectively in your plugin ) ]
版本.yaml
1.0.19:
- 'Updated table hardiksatasiya_demotest_sorting'
- builder_table_update_hardiksatasiya_demotest_sorting_3.php
builder_table_update_hardiksatasiya_demotest_sorting.php
<?php namespace HardikSatasiya\DemoTest\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateHardiksatasiyaDemotestSorting extends Migration
{
public function up()
{
Schema::table('hardiksatasiya_demotest_sorting', function($table)
{
$table->integer('sort_order')->default(0)->change();
});
}
public function down()
{
Schema::table('hardiksatasiya_demotest_sorting', function($table)
{
$table->integer('sort_order')->default(null)->change();
});
}
}
Now, add Trait to your Model
<?php namespace HardikSatasiya\DemoTest\Models;
use Model;
/**
* Model
*/
class Sort extends Model
{
use \October\Rain\Database\Traits\Validation;
use \October\Rain\Database\Traits\Sortable;
....
现在您可以手动添加所有需要的文件或使用构建器工具。
我更喜欢使用 Builder 添加 Controller 和所需文件
Make sure you tick
Reorder behavior
To show name in reorder list, we need to set this property
from which field we need to derive a name
to show in sorting list.
It will look like this with
name
as a sort attribute name
Side Menu [ plugin.yaml and controllers]
如果您还有疑问,请评论。
关于laravel - 重新排序记录显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731016/