dpo69086 2018-06-13 07:48
浏览 79
已采纳

忽略自定义键的独特角色 - Laravel - Mongodb

In my project with Laravel 5.6 and MongoDB, to validate my inputs in an update method, I use a validator like below,

  $validator = Validator::make($request->all(), [
            'name' => 'string|max:255',
            'phone' => 'string|valid_phone',
            'email' => ['string', 'email', 'max:255',
                Rule::unique('admins','email')->ignore($id),
            ],
            'password' => 'string|min:6',
            'access' => 'numeric',
        ]);

I want the field to be unique and ignore the same email for the user with special $id.

Everything looks Ok! But when I call my route to update my user and pass the current email of the user as email, It returns a validator error like this,

"email": [
            "The email has already been taken."
        ]

So, the unique validation did not work correctly!

I also have been set the $primaryKey='_id'; in my user model.

What's the problem? Have I missed something?

  • 写回答

1条回答 默认 最新

  • duanba8173 2018-06-13 08:13
    关注

    If you use $primaryKey='_id' in user model you should set second parameter in ignore method. Below is a quote from documentation:

    If your table uses a primary key column name other than id, you may specify the name of the column when calling the ignore method

     Rule::unique('admins','email')->ignore($id,'_id')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器