Laravel Cloud 来了!为 Laravel 应用提供零配置的托管基础​​设施。 立即部署

Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel

发布日期 经过

Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel image

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

保罗·雷德蒙德照片

Laravel News 特约撰稿人。全栈 Web 开发人员兼作家。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
了解软科技

以每小时 20 美元的价格聘请具备人工智能专业知识的 Laravel 开发人员。48 小时内即可开始工作。

访问 Acquaint Softtech
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire image

Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire

阅读文章
Frontend Nation 2026 Returns June 3-4 with Laravel in the Lineup image

Frontend Nation 2026 Returns June 3-4 with Laravel in the Lineup

阅读文章
Use a Google Sheet as Your Laravel Database with the Google Sheets Database Driver image

Use a Google Sheet as Your Laravel Database with the Google Sheets Database Driver

阅读文章
Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel image

Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel

阅读文章
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

阅读文章
DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell image

DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell

阅读文章