Laravel 5.4 用户模型未更新

Laravel 5.4 User model not updating

我知道这个问题已经被问过十几次了,我已经回答了几次,但 none 似乎解决了我的问题。

我有以下 \App\User 型号:

namespace App;

use Hash;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use \App\Traits\HasUuid;
    use Notifiable;
    use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'zoho_id', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

我正在尝试将其更新为表格。以下 headers 已发送:

------WebKitFormBoundaryb66NOeoCh473wkB7
Content-Disposition: form-data; name="name"

sean
------WebKitFormBoundaryb66NOeoCh473wkB7
Content-Disposition: form-data; name="email"

sean@someplace.net
------WebKitFormBoundaryb66NOeoCh473wkB7
Content-Disposition: form-data; name="roles[]"

2
------WebKitFormBoundaryb66NOeoCh473wkB7
Content-Disposition: form-data; name="zoho"

56511561681616
------WebKitFormBoundaryb66NOeoCh473wkB7
Content-Disposition: form-data; name="password"

------WebKitFormBoundaryb66NOeoCh473wkB7
Content-Disposition: form-data; name="password_confirmation"

------WebKitFormBoundaryb66NOeoCh473wkB7--

这将转到以下 UserController 以更新功能:

public function update(Request $Request, $id) {
        $User = User::findOrFail($id); //Get role specified by id

        //Validate name, email and password fields  
        $validator = Validator::make($Request->all(), [
            'name'      =>  'required|max:120',
            'email'     =>  'required|email|unique:users,email,'.$id,
            'roles.*'   =>  'nullable|exists:roles,id',
            'password'  =>  'nullable|bail|min:6|confirmed'
        ]);

        if ($validator->fails())
            return response()->json($validator->errors(), 400);

        $User->name = $Request->get('name');
        $User->zoho_id = $Request->get('zoho');
        if(!empty($Request->get('password'))){
            $User->password = $Request->get('password');
        }

        try{
            DB::beginTransaction();

            $User->save();

            $User->roles()->sync($Request->get('roles'));  //If one or more role is selected associate user to roles

            return response()->json('"' . $Request->get('name') . '" updated successfully!', 200);      

        } catch(PDOException $e){
            DB::rollback();

            return response()->json('An error occured updating the user.', 500);
        }
    }

它发布到的 URL 是:http://ml.someplace.net/system/users/1/edit

它在我的路由文件中如下:Route::post('/system/users/{user}/edit', 'UserController@update');

我已经完成转储,可以看到模型在保存前后都更新了,但是进入数据库,没有任何更新,包括 "updated_at" 列。

感谢任何对此的建议,我对这个失去了理智。

你错过了 DB::commit() 关注你的 DB::beginTransaction();如果你不包括它,那么什么都不是 "saved",因为交易丢失了(这与 DB::rollBack() 基本相同)

按如下方式调整您的代码:

DB::beginTransaction();

try{
  $User->save();
  $User->roles()->sync($Request->get('roles'));  //If one or more role is selected associate user to roles
} catch(PDOException $e){
  DB::rollback();
  return response()->json('An error occured updating the user.', 500);
}

DB::commit();

return response()->json('"' . $Request->get('name') . '" updated successfully!', 200);

beginTransaction()commit() 可以在 try/catch 的内部或外部,为了便于阅读,我倾向于外部)

有了它,当 try 中的代码顺利通过时,更改将提交到数据库并执行您的 return