marshmallow / nova-sortable
这个 Laravel Nova 扩展包允许您使用拖放功能在 Nova 资源索引视图中重新排序模型。
资助包维护!
marshmallow
Requires
- php: >=8.0
- laravel/nova: ^4.24.0
- outl1ne/nova-translations-loader: ^5.0
- spatie/eloquent-sortable: ^3.10.0|^4.0
- dev-master
- v3.5.0
- v3.4.0
- v3.3.1
- v3.3.0
- 3.2.1
- 3.2.0
- v3.1.0
- 3.0.1
- v3.0.0
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.17
- 1.4.16
- 1.4.15
- 1.4.14
- 1.4.13
- 1.4.12
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- dev-base
- dev-nova4
- dev-dependabot/npm_and_yarn/async-2.6.4
- dev-dependabot/npm_and_yarn/node-forge-1.3.0
- dev-dependabot/npm_and_yarn/minimist-1.2.6
This package is auto-updated.
Last update: 2024-09-22 17:17:02 UTC
README
这是从 outl1ne/nova-sortable 的原始包中分叉出来的。
这个 Laravel Nova 扩展包允许您使用拖放功能在 Nova 资源索引视图中重新排序模型。
底层使用 Spatie 的 eloquent-sortable。
要求
php: >=8.0
laravel/nova: ^4.24.0
功能
- 拖放重新排序(在索引视图或 HasMany 视图中均可)
- 支持 BelongsTo/MorphsTo 重新排序,带有连接表
- 使用起始和结束箭头移动(使项目成为第一个或最后一个)
- 来自 eloquent-sortable 的所有内容
- 本地化
屏幕截图
安装
通过 Composer 在 Laravel Nova 项目中安装该包
# Install package
composer require outl1ne/nova-sortable
使用方法
创建迁移
使用 Laravel 迁移向模型添加一个排序字段
// Add order column to the model Schema::table('some_model', function (Blueprint $table) { $table->integer('sort_order'); }); // Set default sort order (just copy ID to sort order) DB::statement('UPDATE some_model SET sort_order = id');
实现 eloquent-sortable
实现 Spatie 的 eloquent-sortable
接口并应用特性
use Spatie\EloquentSortable\Sortable; use Spatie\EloquentSortable\SortableTrait; class SomeModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, ]; ... }
当模型没有排序配置时,将使用默认的 eloquent-sortable 配置。
将 HasSortableRows 应用于 Nova 资源
在资源上应用此包中的 HasSortableRows
特性
use Outl1ne\NovaSortable\Traits\HasSortableRows; class MyResource extends Resource { use HasSortableRows; ... }
注意!这会覆盖 indexQuery()
方法。
按请求/资源禁用排序
您可以通过覆盖资源上的 canSort()
方法来按请求或资源禁用排序,如下所示
public static function canSort(NovaRequest $request, $resource) { // Do whatever here, ie: // return user()->isAdmin(); // return $resource->id !== 5; return true; }
注意!这需要您禁用缓存(见下文)。
禁用缓存
如果您想禁用缓存(因为使用 canSort
或运行测试),可以在具有 HasSortableRows
特性的资源上将 sortableCacheEnabled
设置为 false。以下是一个示例
class Artist extends Resource { use HasSortableRows; public static $sortableCacheEnabled = false; }
或者,如果您想临时禁用排序缓存(用于测试),可以在资源上调用 Resource::disableSortabilityCache()
。
自定义排序选项
Nova 排序顺序
要在 Nova 中以不同的顺序排序资源,可以在 $sortable
数组中将 nova_order_by
标志设置为 DESC
(默认为 ASC
)。
class SomeModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, 'nova_order_by' => 'DESC', ]; ... }
忽略策略
如果您有一个资源,其 authorizedToUpdate
为 false,但您希望用户仍然能够对其进行排序,则可以使用 ignore_policies
标志,如下所示
class SomeModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, 'ignore_policies' => true, ]; ... }
在 HasMany 关系上排序
注意! 资源只能在 索引视图 或 HasMany 列表视图 中进行排序,但不能同时进行。
在 HasMany 上排序很简单。在模型的 $sortable
数组中将 'sort_on_has_many' => true
添加到模型中。如下所示
public $sortable = [ 'order_column_name' => 'sort_order', 'sort_when_creating' => true, 'sort_on_has_many' => true, ];
排序的 HasMany 配置可以在每个模型级别应用,也可以添加到 eloquent-sortable 的所有模型配置中。
return [ // Spatie sortable configuration /** * Add sort on has many in all the models. **/ 'sort_on_has_many' => true, ];
在 ManyToMany 关系上排序
在 BelongsToMany 和 MorphToMany 关系上排序是可用的,但需要特殊步骤。
请参阅以下文档: 排序 ManyToMany 关系(带有连接表)。
本地化
可以通过以下发布命令发布翻译文件
php artisan vendor:publish --provider="Outl1ne\NovaSortable\ToolServiceProvider" --tag="translations"
您可以通过创建一个新的带有语言环境名称的翻译文件(例如 et.json
),并将现有 en.json
中的 JSON 复制进去,来将翻译添加到 resources/lang/vendor/nova-sortable/
。
其他用法
使用 indexQuery
此包覆盖了资源的 indexQuery
,如果您仍然想使用它,可以按以下方式操作
use HasSortableRows { indexQuery as indexSortableQuery; } public static function indexQuery(NovaRequest $request, $query) { // Do whatever with the query // ie $query->withCount(['children', 'descendants', 'modules']); return parent::indexQuery($request, static::indexSortableQuery($request, $query)); }
致谢
许可证
Nova Sortable 是开源软件,遵循 MIT 许可证。