dongmaxi6763 2019-03-21 10:06
浏览 143

如何根据其他字段规则对唯一数组进行自定义验证规则

I have working on Laravel 5.6. I have 4 dependent unique columns but don't know how to validate these dependent columns here is my migration for unique constrain:

$table->unique(array('lvl4','document_type','nature_id','type_id'),'u_coa_lvl4_asn_dnt_uk');

lvl4 is array.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dos71253 2019-03-22 05:19
    关注

    You can try manaully validating with something like this:

    $data = [
        'lvl4' => 'Level 4',
        'document_type' => 'Some type',
        'nature_id' => 1,
        'type_id' => 7,
    ];
    
    $validator = Validator::make($data, [
        'data.lvl4' => [
            'required',
            Rule::unique('your_table')->where(function ($query) use($data) {
                return $query->where('lvl4', $data['lvl4'])
                    ->where('document_type', $data['document_type'])
                    ->where('nature_id', $data['nature_id'])
                    ->where('type_id', $data['type_id']);
            }),
        ],
    ]);
    
    if($validator->fails()) {
        // it fails validation ...
    }
    

    This is untested, but something like this should work for you.

    https://laravel.com/docs/5.6/validation#rule-unique

    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表