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

忽略自定义键的独特角色 - 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?

图片转代码服务由CSDN问答提供 功能建议

在我的 Laravel 5.6 MongoDB 的项目中, 在更新方法中验证我的输入,我使用如下的验证器,

  $ validator = Validator :: make($ request-> all(),[
 n'  name'=>'string | max:255',
'phone'=>'string | valid_phone',
'email'=> ['string','email','max:255',  
规则::唯一('admins','email') - >忽略($ id),
 
,
'密码'=>'字符串| min:6',
'访问'=  >'数字',
]); 
   
 
 

我希望该字段是唯一的,并为具有特殊 $ id的用户忽略相同的电子邮件

一切看起来都好! 但当我调用我的路由来更新我的用户并将用户的当前电子邮件作为电子邮件传递时,它会返回一个验证器错误,如下所示,

 “email”:[
  “电子邮件已被删除。”
] 
   
 
 

因此,唯一验证无法正常运行!

我还在我的用户模型中设置了 $ primaryKey ='_ id';

有什么问题? 我错过了什么吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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')
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题