更新时插入错误

时间:2014-04-28 05:14:58

标签: php laravel eloquent

我对MVC有很多经验,但似乎无法弄清楚这里出了什么问题。 我试图更新用户的模型,但由于某种原因,更新应该尝试作为插入执行。

编辑:我使用this starter site为使用Ardent的Laravel

$user = User::find(Auth::user()->id);
$user->recipient_id = "xxxxxxxxx";
if ($user->save()) {
    return true;
} else {
    print_r($user->errors()->all()); die();
}

以上输出

Array (
    [0] => The username has already been taken.
    [1] => The email has already been taken.
    [2] => The password confirmation does not match.
)

任何帮助都会非常感激!我感觉它有些微不足道......

1 个答案:

答案 0 :(得分:0)

实际上,在验证它时会使用User Model类的规则。 这是因为在模型中使用这些行。

public $autoHydrateEntityFromInput = true;    // hydrates on new entries' validation
public $forceEntityHydrationFromInput = true; // hydrates whenever validation is called

您需要做的是动态更改规则。

$user = User::find(Auth::user()->id);
$user->recipient_id = "xxxxxxxxx";
$user::$rules['username'] = 'required|regex:/^[a-zA-Z\- ]+$/|unique:users,username,'.$user_id;
$user::$rules['email'] = 'required|email|unique:users,email,'.$user_id;
$user::$rules['password'] = 'required';
//this will ignore current username and email
if ($user->save()) {
    return true;
} else {
    print_r($user->errors()->all()); die();
}