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 ENVI高分五号去除云层的方法
  • ¥15 16进制数据如何得到奇偶校验位
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系