当前位置:网站首页>laravel 开发 文章URL 生成器
laravel 开发 文章URL 生成器
2022-06-22 10:26:00 【rorg】
当我们处理用户或帖子时,我们经常需要的功能之一是生成用户名或 slug。 然而,自动处理重复并不总是那么简单。 让我们看看如何使用一个非常简单的解决方案:文章URL 生成器。

持久化模型时生成用户名
假设我们想在将模型存储到数据库时自动从用户名生成用户名(或 slug)。 通常,这没什么大不了的,我们可以使用一个简单的 Eloquent 事件回调和 Str::slug() 助手
class User extends Authenticatable implements MustVerifyEmail
{
protected static function booted(): void
{
static::creating(static function (self $user): void {
$user->username = Str::slug($user->name, '.');
});
}
}对于slugs,通常我们使用–作为分隔符,但对于用户名,它更可能是.
这段代码运行良好,干净且富有表现力。但是,如果有两个同名用户怎么办?用户名应该是唯一的。
生成增量用户名或URL
使用非常简单的正则表达式模式,我们可以轻松地跟踪数据库中相似的用户名,并为用户名添加或增加数字后缀。让我们看一下代码并更新模型中的创建回调
protected static function booted(): void
{
static::creating(static function (self $user): void {
$user->username = static::generateUniqueUsername(
Str::slug($user->name, '.')
);
});
}
protected static function generateUniqueUsername(string $value): string
{
$username = static::query()
->where('username', 'regexp', preg_quote($value).'[\\d]?$')
->orderByDesc('username')
->take(1)
->value('username');
$number = (int) filter_var($username, FILTER_SANITIZE_NUMBER_INT);
return $number === 0 ? $value : $value.++$number;
}注意,在 MySQL 中我们需要使用双反斜杠:\\
在代码中,我们正在搜索与当前生成的用户名匹配的“最新”用户名,并在需要时添加或增加数字前缀。因此它会自动生成以下模式:用户名、用户名1、用户名2、用户名3,等等
总结
这是一个非常简单的解决方案,对于更简单的应用程序应该可以很好地工作。但是,当您需要更复杂的 slug 或用户名解决方案时,您可以扩展此解决方案或引入一个包
边栏推荐
- 使用 Matplotlib 这么久,竟不知道数据可以动起来
- 批量创建/删除文件、文件夹、修改文件名 后缀名
- 【深度学习】不得了!新型Attention让模型提速2-4倍!
- 这不会又是一个Go的BUG吧?
- 【这款工具配合jmeter,会让你的工作效率至少提升80%,强烈推荐给大家】
- Open a safe distance: the international space station has taken measures to actively avoid space debris
- 等重构完这系统,我就提离职!
- 工作中的帕累托定律:关注结果,而不是输出
- 力扣 1108. IP 地址无效化
- Quel est le risque de divulgation d'un certificat de signature de code?
猜你喜欢

Summary of neural network training trick

扎克伯格最新VR原型机来了,要让人混淆虚拟与现实的那种

Gartner表示:云数据库发展强劲,但本地数据库仍然充满活力

【这款工具配合jmeter,会让你的工作效率至少提升80%,强烈推荐给大家】

Who says PostgreSQL has no reliable high availability (2)

力扣 1108. IP 地址无效化

The data intelligence infrastructure upgrade window is approaching? See Chapter 9 how Yunji dingodb breaks through data pain points

2022年深入推进IPv6部署和应用,该如何全面实现安全升级改造?

Vs2022 connecting to SQLSERVER database tutorial

关于 GIN 的路由树
随机推荐
scrapy.Request() 的 meta参数 数据的传递
电装中国采用 Oracle HCM 云技术解决方案加速人力资源数字化转型
网络中connect的作用
从MVC原理开始手敲一个MVC框架,带你体会当大神的乐趣
Solend abolishes the proposal to "take over the giant whale" and liquidates the "bomb"
Open a safe distance: the international space station has taken measures to actively avoid space debris
2022-06-09 工作记录--yarn/npm-Error-EPERM: operation not permitted, uv_cwd
定金预售的规则思路详解
TCP建立连接过程(深入源码理解3次握手)
Good news - agile technology was selected into the 2022 China top 100 Digital Security Report
2022年深入推进IPv6部署和应用,该如何全面实现安全升级改造?
logstash中Ruby代码把@timestamp时间戳格式转换
如何进行高效简洁的电子文档管理
中年失业是一种什么体验
牛客网——华为题库(31~40)
这不会又是一个Go的BUG吧?
Encryption market plummeted, Seth triggered a new round of concern
被曝泄露超 1.7 亿条隐私数据,学习通回应:尚未发现明确证据
麒麟软件携手格尔软件聚焦网络数据安全发展
AttributeError: module ‘skimage.draw‘ has no attribute ‘circle‘