reinbier/laravel-unique-with
Composer 安装命令:
composer require reinbier/laravel-unique-with
包简介
Unique With Validator rule for Laravel
README 文档
README
This package contains a variant of the validateUnique rule for Laravel, that allows for validation of multi-column UNIQUE indexes.
Please note
This package is to continue development of the package uniquewith-validator by Felix Kiss and have it work with recent versions of the Laravel framework (continuing from Laravel 9 onwards). For older versions of the framework, please use the aforementioned package.
Versioning
For the sake of maintainability and testability, this package uses different major versions to support Laravel, make sure to pick the right one based on the table below.
| Laravel version | Package version |
|---|---|
| v9 & 10 | v1.x |
| v11 & 12 | v2.x |
| v13 | v2.1.x |
Installation
You can install the package via composer:
composer require reinbier/laravel-unique-with
Usage
Use it like any Validator rule:
$rules = [ '<field1>' => 'unique_with:<table>,<field2>[,<field3>,...,<ignore_rowid>]', ];
See the Validation documentation of Laravel.
Specify different column names in the database
If your input field names are different from the corresponding database columns, you can specify the column names explicitly.
e.g. your input contains a field 'last_name', but the column in your database is called 'sur_name':
$rules = [ 'first_name' => 'unique_with:users, middle_name, last_name = sur_name', ];
Ignore existing row (useful when updating)
You can also specify a row id to ignore (useful to solve unique constraint when updating)
This will ignore row with id 2
$rules = [ 'first_name' => 'required|unique_with:users,last_name,2', 'last_name' => 'required', ];
To specify a custom column name for the id, pass it like
$rules = [ 'first_name' => 'required|unique_with:users,last_name,2 = custom_id_column', 'last_name' => 'required', ];
If your id is not numeric, you can tell the validator
$rules = [ 'first_name' => 'required|unique_with:users,last_name,ignore:abc123', 'last_name' => 'required', ];
Add additional clauses (e.g. when using soft deletes)
You can also set additional clauses. For example, if your model uses soft deleting then you can use the following code to select all existing rows but marked as deleted
$rules = [ 'first_name' => 'required|unique_with:users,last_name,deleted_at,2 = custom_id_column', 'last_name' => 'required', ];
Soft delete caveat:
If the validation is performed in a form request class, field deleted_at is skipped, because it's not send in request. To solve this problem, add 'deleted_at' => null to your validation parameters in request class., e.g.:
protected function validationData() { return array_merge($this->request->all(), [ 'deleted_at' => null ]); }
Specify specific database connection to use
If we have a connection named some-database, we can enforce this connection (rather than the default) like this:
$rules = [ 'first_name' => 'unique_with:some-database.users, middle_name, last_name', ];
Example
Pretend you have a users table in your database plus User model like this:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->timestamps(); $table->string('first_name'); $table->string('last_name'); $table->unique(['first_name', 'last_name']); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } }
<?php class User extends Model { }
Now you can validate a given first_name, last_name combination with something like this:
Route::post('test', function() { $rules = [ 'first_name' => 'required|unique_with:users,last_name', 'last_name' => 'required', ]; $validator = Validator::make(Input::all(), $rules); if($validator->fails()) { return Redirect::back()->withErrors($validator); } $user = new User; $user->first_name = Input::get('first_name'); $user->last_name = Input::get('last_name'); $user->save(); return Redirect::home()->with('success', 'User created!'); });
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
The code from this package is based on the original code that came from uniquewith-validator by Felix Kiss.
License
The MIT License (MIT). Please see License File for more information.
reinbier/laravel-unique-with 适用场景与选型建议
reinbier/laravel-unique-with 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 121.71k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Reinbier」 「laravel-unique-with」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 reinbier/laravel-unique-with 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 reinbier/laravel-unique-with 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 reinbier/laravel-unique-with 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Holidays in Laravel, the right way.
For using & generating TailwindCSS colors in Laravel
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
统计信息
- 总下载量: 121.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-31