ankitsinghdalal/laravel-unique-on-multiple
Composer 安装命令:
composer require ankitsinghdalal/laravel-unique-on-multiple
包简介
This package would serve the purpose of unique validator against multiple columns
README 文档
README
You can install the package via composer:
composer require ankitsinghdalal/laravel-unique-on-multiple
The package will automatically register itself.
Usage
Inside app/Http/Requests/Something/StoreSometing.php
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required|max:225|uniqueOfMultiple:menus,location_id-' . $this->get('location_id', 'NULL') . ',language_id-' . $this->get('language_id', 1), 'location_id' => 'required|exists:menu_location,id', 'order' => 'digits_between:0,10' ]; }
Inside app/Http/Requests/Something/UpdateSomething.php
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required|max:225|uniqueOfMultiple:menus,location_id-' . $this->get('location_id', 'NULL') . ',language_id-' . $this->get('language_id', 'NULL') . ',except-id-' . $this->route('id', 'NULL'), 'location_id' => 'required|exists:menu_location,id', 'order' => 'digits_between:0,10' ]; }
Inside resources/lang/en/validation.php
'unique_of_multiple' => 'The :attribute has already been taken under its parent.',
In the above written piece of code, the custom validation used is uniqueOfMultiple. The first argument passed is the table_name i.e menus and all other arguments are column_name(s) that are comma-separated. The columns used while creating the resoource are name (primary column), location_id, and language_id. However updating a resource uses one extra except-for condition i.e except-id. The respective value passed for each field is - separated.
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-16