请问联合唯一索引的 unique 规则该怎么写?

unique 验证规则

unique:table,column,except,idColumn

对单个字段验证唯一性很有用,但是对联合唯一索引,该怎么去写验证规则呢?
例如下表:

CREATE TABLE `configs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `brand_id` int(10) unsigned NOT NULL COMMENT '品牌ID',
  `modelpn_id` int(10) unsigned NOT NULL COMMENT 'modelpn ID',
  `country_id` int(10) unsigned NOT NULL COMMENT '国家ID',
  `created` int(11) NOT NULL COMMENT '创建时间',
  `updated` int(11) DEFAULT NULL COMMENT '更新时间',
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0:正常 1:删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `configs_brand_id_modelpn_id_country_id_unique` (`brand_id`,`modelpn_id`,`country_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='联保配置';

brand_idmodelpn_idcountry_id 三个字段建唯一索引,在表单验证的时候,unique 验证规则该如何去写呢?
实在不行就只能在写入数据库前,去查是否有这条数据了。

为了点个赞,专门注册的账号
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 7
终生学习者

你的问题解决了嘛?? 我也遇到同样的问题了

5年前 评论

可以用 验证规则的 sometimes 进行多字段 自定义验证
表单验证《Laravel 5.6 中文文档》

$v = Validator::make($request->all(),[
            'country_id'=>'required'
        ]);
$v->sometimes(['country_id','brand_id','modelpn_id'], 'required|numeric', function ($input) {
    $configs =  Config::where([
        'brand_id' => $input->brand_id,
        'modelpn_id' => $input->modelpn_id,
        'country_id' => $input->country_id
    ])->first();
    return $configs?false:true;
});
if($v->fails()) {
    return response($v->errors());
}

期待大神更好的方法!!

5年前 评论
终生学习者

@dli 如何结合,controller 已经实例化的 $this->validator 啊??

5年前 评论

@一个菜鸟 还可以用Rule 规则

// controller 里面
$where = [
        'brand_id' => $request->get('brand_id'),
        'modelpn_id' => $request->get('modelpn_id'),
    ];
$this->validate($request,[
    "country_id" => [
        "required",
        Rule::unique('configs')
            ->where(function($query) use($where){
                return $query->where($where);
            })
    ]
]);  

翻了下 controller 里面的 validate

    public function validate(Request $request, array $rules,
                             array $messages = [], array $customAttributes = [])
    {
        return $this->getValidationFactory()->make(
            $request->all(), $rules, $messages, $customAttributes
        )->validate();
    }

直接用 $request 对象 的validate 都是一样的

$where = [
        'brand_id' => $request->get('brand_id'),
        'modelpn_id' => $request->get('modelpn_id'),
    ];
$request->validate([
     "country_id" => [
         "required",
         Rule::unique('configs')
             ->where(function($query) use($where){
                 return $query->where($where);
             })
         ]
]);

这是 执行的sql 贴出来

select count(*) as aggregate from `configs` where `country_id` = '1' and ((`brand_id` = '2' and `modelpn_id` = '1'))
5年前 评论
终生学习者

@dli 非常谢谢你的帮助3Q

5年前 评论

我的是这样

 public function rules()
    {
        return [
            'name' => [
                'required',
                Rule::unique('post_tags', 'name')->where(function ($query) {
                    return $query->where('post_id', $this->route('post')->id);
                }),
            ],
        ];
    }
3年前 评论

@小李世界 如果更新忽略本条怎么写呢?ignore($tag_id),$tag_id在FormRequest里面不知道咋获取

2年前 评论
小李世界 2年前
发呆的小孩 (作者) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!