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')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?