Save方法不会更新DB中已存在的模型

时间:2016-07-03 18:47:33

标签: php laravel laravel-5.2

我正确检索了数据库中的记录,并在textfield中打印出值。但是,当我想更新我的数据库时,它将无法正常工作。它在文档中说

  

save()方法也可用于更新已存在的模型   在数据库中。要更新模型,您应该检索它。

所以我有两个刀片模板显示编辑。第一个刀片显示数据库中存在的值(GET REQUEST)。第二个刀片显示用户更新数据的编辑表单。 (后请求)。

show.blade.php

<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.show', $result->id) }}">

<h3>View Employee</h3>
<hr>

<div class = "form-group">

    <label for = "email" class = "control-label">Email Address</label>
    <input type = "text" name = "email" class = "form-control" placeholder = "{{ $result->email }}" readonly>

</div>

<div class = "form-group">

    <label for = "username" class = "control-label">Username</label>
    <input type = "text" name = "username" class = "form-control" placeholder = "{{ $result->username }}" readonly>

</div>

<input type = "hidden" name = "_token" value = "{{ Session::token() }}">

</form>

edit.blade.php

<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.edit', $result->id) }}">

<h3>Edit Employee</h3>
<hr>

<div class = "form-group">

    <label for = "email" class = "control-label">Email Address</label>
    <input type = "text" name = "email" class = "form-control" value = "{{ $result->email }}">

</div>

<div class = "form-group">

    <label for = "username" class = "control-label">Username</label>
    <input type = "text" name = "username" class = "form-control" value = "{{ $result->username }}">

</div>

<div class = "form-group">

    <button type = "submit" class = "btn btn-success">Save</button>

</div>

<input type = "hidden" name = "_token" value = "{{ Session::token() }}">

</form>

控制器:

//SHOW
public function showEmployee($id)
{
    $result = User::find($id);

                                        //key     //value
    return view ('account.show')->with('result', $result);
}

//EDIT
public function editEmployee($id)
{

    $result = User::find($id);

    return view ('account.edit')->with('result', $result);
}

//UPDATE
public function updateEmployee(Request $request, $id)
{
    $this->validate($request, [
                            //This will be unique in users table
        'email' => 'required|unique:users,email,'.$id.'|email|max:255',
        'username' => 'required|unique:users,username,'.$id.'|alpha|max:20',
    ]);

    $email = $request['email'];
    $username = $request['username'];

    $user = User::findOrFail($id); 
    $user->email = $email; //Accessing properties of Model
    $user->username = $username;

    $user->save();

    return view ('account.show');
}

路线:

//SHOW
Route::get('/show/{id}',
[
    'uses' => '\App\Http\Controllers\AccountController@showEmployee',
    'as' => 'account.show',
]);

//EDIT
Route::get('/edit/{id}',
[
    'uses' => '\App\Http\Controllers\AccountController@editEmployee',
    'as' => 'account.edit',
]);


//UPDATE
Route::post('/edit/{id}',
[
    'uses' => '\App\Http\Controllers\AccountController@updateEmployee',
    'as' => 'account.edit',
]);

2 个答案:

答案 0 :(得分:1)

您在这里创建了一个新的User

要更新现有数据,您需要先加载要编辑的用户数据。您可以执行此操作并通过更改

来解决该问题
$user = new User();

$user = User::findOrFail($id); 

(假设$id是该用户的ID)

此外,如果用户名和电子邮件都没有更改,那么您的验证将失败,因为您正在检查这两个条目的唯一性,并且当前用户已经拥有该用户名或电子邮件。

要解决此问题,您需要从检查中排除当前用户以进行验证,

  'email' => 'required|unique:users,email,'.$id.'|email|max:255',
  'username' => 'required|unique:users,username,'.$id.'|alpha|max:20',

有关详细信息,请参阅&#39;强制使用唯一规则忽略给定的ID&#39;在此处的文档中 - Validation - unique rule

答案 1 :(得分:0)

您的updateEmployee方法未使用提供的$id参数,您只需创建一个新的用户实例并进行保存,而无需检索现有模型并进行更新:

$user = User::find($id);// retrieve the existing user
$user->email = $email;
$user->username = $username;

$user->save();// update the record in DB