简体   繁体   English

Laravel 5表单更新请求

[英]Laravel 5 form request on update

I don't understand why this doesn't work, i have searched the forums and read the official documentation, obviously I'm making a silly mistake: 我不明白为什么这行不通,我已经搜索了论坛并阅读了官方文档,显然我犯了一个愚蠢的错误:

public function rules()
{
    $customer = Customer::find($this->customers);
    return [
        'data.name'       => 'required|unique:customers,name,1'
    ];
}

That is a CustomerUpdateFormRequest that i created, i manually typed the customer id which is 1 in the rule, also with: 'data.name' => 'required|unique:customers,name,'.$customer->id, 那是我创建的CustomerUpdateFormRequest,我在规则中手动键入了1的客户ID,同时还输入了: 'data.name' => 'required|unique:customers,name,'.$customer->id,

The problem is that it still doesnt pass the unique validation, i giving me the Customer already exists error even after manually or dynamically type the customer id. 问题是它仍然没有通过唯一验证,即使手动或动态键入客户ID后,我也会给我“客户已存在”错误。

I read this http://laravel.com/docs/5.0/validation and https://laracasts.com/discuss/channels/requests/laravel-5-validation-request-how-to-handle-validation-on-update and I 'Believe' that my code is correct, why is not working? 我阅读了此http://laravel.com/docs/5.0/validationhttps://laracasts.com/discuss/channels/requests/laravel-5-validation-request-how-to-handle-validation-on-update我“相信”我的代码是正确的,为什么不起作用?

The code above is working just fine, no syntax errors or logic, the problem was in the database, there was another customer with the same data inserted i guess before the validation form was created. 上面的代码工作正常,没有语法错误或逻辑,问题出在数据库中,我猜想在创建验证表单之前,有另一位客户插入了相同的数据。

I dont know if i should delete this question or leave it here, anyway the code its fine if u have any problems like mine PLEASE CHECK YOUR DB. 我不知道我是否应该删除此问题或将其留在这里,无论如何,如果您遇到像我这样的任何问题,请仔细检查您的数据库。

Thanks! 谢谢!

public function rules()
{

    $customer= $this->route('customer'); //$this->('route-name')

    return [
        'data.name'       => 'required|unique:customers,name,' . $customer->id
    ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM