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

Laravel Sluggable

发布日期 经过

Laravel Sluggable image

努诺·马杜罗 发布 Laravel Sluggable , a package for automatic slug generation on Eloquent models. Rather than reaching for a trait or a base class, the package uses a single PHP attribute #[Sluggable] , placed directly on the model.

Beyond the basics, it covers the edge cases that tend to surface in real apps: collision handling, scoped uniqueness (per-tenant, per-locale), multi-column sources, soft-deleted record collisions, and full Unicode and CJK transliteration.

立即开始

通过 Composer 安装软件包:

作曲家 要求 nunomaduro/laravel-sluggable

笔记: This package requires PHP 8.5+ and Laravel 13.5+

The quickest way to wire up a model is the included make:sluggable Artisan 命令:

php 工匠 make:sluggable 邮政

It reads your model's table schema, picks the most likely source column ( title , name , headline , 或者 subject ), adds the #[Sluggable] attribute to the model class, and creates a migration in database/migrations :

使用 NunoMaduro\LaravelSluggable\Attributes\Sluggable ;
#[ Sluggable : '标题' )]
班级 邮政 延伸 模型
{
}
架构 :: 桌子 '帖子' , 功能 蓝图 $table) {
$表
-> 细绳 'slug'
// ->nullable()
-> 独特的 ()
-> 'ID' (英文):
});

Review the migration before running it. You may need to make the column nullable if the table already has rows before you run php artisan migrate

After that, slug generation is automatic:

$post = 邮政 :: 创造 ([ '标题' => 'Laravel News Rocks' ]);
$post -> slug; // "laravel-news-rocks"

配置

All options live on the attribute itself. The fromto parameters control which columns are read and written, and from accepts an array when you want to combine multiple columns:

#[ Sluggable :[ '名' , '姓' ], : 'slug' )]
班级 作者 延伸 模型 {}
$author = 作者 :: 创造 ([ '名' => “约翰” , '姓' => 'Tolkien' ]);
$author -> slug; // "john-tolkien"

For multi-tenant apps or anything with per-scope uniqueness, pass a scope column (or an array of columns):

#[ Sluggable : '标题' , 范围 : 'team_id' )]

Other notable options include maxLength to cap slug length, separator to swap out the dash, and onUpdating to regenerate the slug whenever the source column changes. By default, slugs are locked after creation — manually assigned slug values are always preserved regardless of this setting.

错误处理

If a slug cannot be generated, the package throws a CouldNotGenerateSlugException . In HTTP contexts, it automatically renders as a 422 response with a validation-style error payload, so it drops into existing error handling without extra work. The error key defaults to the source column name but can be overridden via errorKey . Because it extends ValidationException , it won't show up in your application logs.

Unicode Support

The pipeline transliterates non-Latin scripts to readable Latin slugs and is domain-aware — values that look like hostnames or file paths keep their dots intact instead of being collapsed to dashes.

You can learn more about Laravel Sluggable on the projects GitHub repo.

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

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

图像
了解软科技

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

访问 Acquaint Softtech
MongoDB 徽标

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB
鱼叉:新一代时间跟踪和发票标志

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

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

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

PhpStorm

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

PhpStorm
Shift 标志

转移

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

转移
Lucky Media 标志

幸运传媒

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

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

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

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

几天内即可获得 Laravel 代码审查方面的专家指导
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

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

SaaSykit:Laravel SaaS 入门套件
Tinkerwell 徽标

廷克威尔

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

廷克威尔
Kirschbaum 标志

樱桃树

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

樱桃树
了解 Softtech 的标志

了解软科技

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

了解软科技
Laravel Cloud 标志

Laravel 云

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

Laravel 云
RedBerry to Host Georgia's First Laravel Meetup in Tbilisi image

RedBerry to Host Georgia's First Laravel Meetup in Tbilisi

阅读文章
Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

阅读文章
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

阅读文章
Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

阅读文章
Polyscope for Windows is Now Available image

Polyscope for Windows is Now Available

阅读文章
Ship AI with Laravel: RAG with Embeddings and pgvector in Laravel 13 image

Ship AI with Laravel: RAG with Embeddings and pgvector in Laravel 13

阅读文章