Laravel Reorderable by Richie McMullen adds drag-and-drop sorting to any Eloquent model. It ships with ready-made Blade and Livewire components, persists new positions automatically via a package route, and supports scoping sort order within a parent group.
This package works by applying the
HasSortOrder
trait and
ReorderableContract
to your model:
使用
Atomcoder\LaravelReorderable\Contracts\ReorderableContract
;使用
Atomcoder\LaravelReorderable\Traits\HasSortOrder
;班级
任务
延伸
模型
实现
ReorderableContract{
使用
HasSortOrder
;
受保护
$可填充
=
[
'标题'
,
'project_id'
,
'sort_order'
];
受保护
细绳
$sortColumn
=
'sort_order'
;
民众
功能
getReorderLabel
()
:
细绳{
返回
$this
->
标题;}}
The trait automatically assigns a sort position on creation and adds an
ordered()
query scope so you fetch records in the right sequence.
Blade and Livewire Components
The package covers both rendering approaches. Drop an
@include
into a Blade view, or use the Livewire component — both accept the same options:
Blade:
@包括
(
'reorderable::components.list'
,[
'项目'
=>
$tasks,
'modelClass'
=>
应用程序
\
楷模
\
任务
::班级
,
'groupColumn'
=>
'project_id'
,
'groupValue'
=>
$project
->
ID,
'标题'
=>
'Reorder Tasks'
,
'listId'
=>
'project-tasks-list'
,])
Livewire:
<
livewire:reorderable-list
:items
=
"$tasks"
model-class
=
"App\Models\Task"
group-column
=
"project_id"
:group-value
=
"$project->id"
标题
=
"Reorder Tasks"
list-id
=
"project-tasks-list"/>
When a user drags an item, the component posts the new order to
POST /reorderable/update
, which updates the sort column for each record in the database.
Grouped Reordering
The package restricts sort operations to a parent group, so dragging tasks in one project doesn't affect tasks in another. The same scoping is available programmatically via
moveToPosition()
或者
reorderFromArray()
:
任务
->
moveToPosition
(
位置
:
2
,
groupColumn
:
'project_id'
,
groupValue
: $task
->
project_id,(英文):任务
::
reorderFromArray
(
排序后的ID
:[
8
,
3
,
5
,
1
],
groupColumn
:
'project_id'
,
groupValue
:
12
,(英文):
The Generator Command
Instead of wiring up the trait and configuration by hand,
reorderable:make
scaffolds everything from the command line:
php
工匠
reorderable:make
任务
--table=tasks
--label=title
--column=sort_order
这
--label
option controls which attribute shows in the drag-and-drop UI, and
--column
sets the sort column name if you're not using the default
sort_order
。
Authorization and Events
The configuration accepts a closure to gate access to the update route, so you can tie it to a policy or role check:
'authorize'
=>
功能
(请求,
细绳
$modelClass)
:
布尔值
{
返回
$请求
->
用户
()
?->
能
(
'manage-content'
)
??
错误的
;},
After a successful reorder, the package dispatches
ItemsReordered
, which carries the model class, the ordered IDs, and the group column and value — useful for cache busting or audit logging.
要求
The package requires PHP 8.3+, Laravel 13+, and Livewire 4+.
To learn more, view the source on GitHub 。






