codezero/laravel-unique-translation
Composer 安装命令:
composer require codezero/laravel-unique-translation
包简介
Check if a translated value in a JSON column is unique in the database.
关键字:
README 文档
README
IMPORTANT: March 2022
It's horrible to see what is happening now in Ukraine, as Russian army is bombarding houses, hospitals and kindergartens.
Please check out supportukrainenow.org for the ways how you can help people there. Spread the word.
And if you are from Russia and you are against this war, please express your protest in some way. I know you can get punished for this, but you are one of the hopes of those innocent people.
Check if a translated value in a JSON column is unique in the database.
Imagine you want store a slug for a Post model in different languages.
The amazing spatie/laravel-translatable package makes this a cinch!
But then you want to make sure each translation is unique for its language.
That's where this package comes in to play.
This package also supports spatie/nova-translatable in case you are using Laravel Nova and filamentphp/spatie-laravel-translatable-plugin in case you are using Filament.
✅ Requirements
- PHP ^7.2 or PHP ^8.0
- MySQL >= 5.7
- Laravel >= 6
- spatie/laravel-translatable ^4.4|^5.0|^6.0
- spatie/nova-translatable ^3.0
- filamentphp/spatie-laravel-translatable-plugin ^3.0
📦 Installation
Require the package via Composer:
composer require codezero/laravel-unique-translation
Laravel will automatically register the ServiceProvider.
🛠 Usage
For the following examples, I will use a slug in a posts table as the subject of our validation.
☑️ Validate a Single Translation
Your form can submit a single slug:
<input name="slug">
We can then check if it is unique in the current locale:
$attributes = request()->validate([ 'slug' => 'required|unique_translation:posts', ]);
You could also use the Rule instance:
use CodeZero\UniqueTranslation\UniqueTranslationRule; $attributes = request()->validate([ 'slug' => ['required', UniqueTranslationRule::for('posts')], ]);
☑️ Validate an Array of Translations
Your form can also submit an array of slugs.
<input name="slug[en]"> <input name="slug[nl]">
We need to validate the entire array in this case. Mind the slug.* key.
$attributes = request()->validate([ 'slug.*' => 'unique_translation:posts', // or... 'slug.*' => UniqueTranslationRule::for('posts'), ]);
☑️ Specify a Column
Maybe your form field has a name of post_slug and your database field slug:
$attributes = request()->validate([ 'post_slug.*' => 'unique_translation:posts,slug', // or... 'post_slug.*' => UniqueTranslationRule::for('posts', 'slug'), ]);
☑️ Specify a Database Connection
If you are using multiple database connections, you can specify which one to use by prepending it to the table name, separated by a dot:
$attributes = request()->validate([ 'slug.*' => 'unique_translation:db_connection.posts', // or... 'slug.*' => UniqueTranslationRule::for('db_connection.posts'), ]);
☑️ Ignore a Record with ID
If you're updating a record, you may want to ignore the post itself from the unique check.
$attributes = request()->validate([ 'slug.*' => "unique_translation:posts,slug,{$post->id}", // or... 'slug.*' => UniqueTranslationRule::for('posts')->ignore($post->id), ]);
☑️ Ignore Records with a Specific Column and Value
If your ID column has a different name, or you just want to use another column:
$attributes = request()->validate([ 'slug.*' => 'unique_translation:posts,slug,ignore_value,ignore_column', // or... 'slug.*' => UniqueTranslationRule::for('posts')->ignore('ignore_value', 'ignore_column'), ]);
☑️ Use Additional Where Clauses
You can add 4 types of where clauses to the rule.
where
$attributes = request()->validate([ 'slug.*' => "unique_translation:posts,slug,null,null,column,value", // or... 'slug.*' => UniqueTranslationRule::for('posts')->where('column', 'value'), ]);
whereNot
$attributes = request()->validate([ 'slug.*' => "unique_translation:posts,slug,null,null,column,!value", // or... 'slug.*' => UniqueTranslationRule::for('posts')->whereNot('column', 'value'), ]);
whereNull
$attributes = request()->validate([ 'slug.*' => "unique_translation:posts,slug,null,null,column,NULL", // or... 'slug.*' => UniqueTranslationRule::for('posts')->whereNull('column'), ]);
whereNotNull
$attributes = request()->validate([ 'slug.*' => "unique_translation:posts,slug,null,null,column,NOT_NULL", // or... 'slug.*' => UniqueTranslationRule::for('posts')->whereNotNull('column'), ]);
☑️ Laravel Nova
If you are using Laravel Nova in combination with spatie/nova-translatable, then you can add the validation rule like this:
Text::make(__('Slug'), 'slug') ->creationRules('unique_translation:posts,slug') ->updateRules('unique_translation:posts,slug,{{resourceId}}');
☑️ Filament
If you are using Filament in combination with filamentphp/spatie-laravel-translatable-plugin, then you can add the validation rule like this:
TextInput::make('slug') ->title(__('Slug')) ->rules([ UniqueTranslationRule::for('posts', 'slug') ])
TextInput::make('slug') ->title(__('Slug')) ->rules([ fn (Get $get) => UniqueTranslationRule::for('posts', 'slug')->ignore($get('id')) ])
🖥 Example
Your existing slug column (JSON) in a posts table:
{
"en":"not-abc",
"nl":"abc"
}
Your form input to create a new record:
<input name="slug[en]" value="abc"> <input name="slug[nl]" value="abc">
Your validation logic:
$attributes = request()->validate([ 'slug.*' => 'unique_translation:posts', ]);
The result is that slug[en] is valid, since the only en value in the database is not-abc.
And slug[nl] would fail, because there already is a nl value of abc.
⚠️ Error Messages
You can pass your own error messages as normal.
When validating a single form field:
<input name="slug">
$attributes = request()->validate([ 'slug' => 'unique_translation:posts', ], [ 'slug.unique_translation' => 'Your custom :attribute error.', ]);
In your view you can then get the error with $errors->first('slug').
Or when validation an array:
<input name="slug[en]">
$attributes = request()->validate([ 'slug.*' => 'unique_translation:posts', ], [ 'slug.*.unique_translation' => 'Your custom :attribute error.', ]);
In your view you can then get the error with $errors->first('slug.en') (en being your array key).
🚧 Testing
vendor/bin/phpunit
☕️ Credits
🔓 Security
If you discover any security related issues, please e-mail me instead of using the issue tracker.
📑 Changelog
A complete list of all notable changes to this package can be found on the releases page.
📜 License
The MIT License (MIT). Please see License File for more information.
codezero/laravel-unique-translation 适用场景与选型建议
codezero/laravel-unique-translation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.03M 次下载、GitHub Stars 达 187, 最近一次更新时间为 2017 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「json」 「translation」 「mysql」 「validator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codezero/laravel-unique-translation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codezero/laravel-unique-translation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codezero/laravel-unique-translation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 1.03M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 189
- 点击次数: 24
- 依赖项目数: 10
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-22
