Laravel 5.4用户模型未更新

时间:2018-07-10 03:59:54

标签: model controller laravel-5.4

我知道这个问题已经被问过十二遍了,我经历了几次,但似乎没有一个能解决我的问题。

我具有以下\ 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);
    }

,我正在尝试将其更新为表格。发送以下标头:

------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”列。

对此有任何建议,不胜感激。

1 个答案:

答案 0 :(得分:1)

您在DB::commit()之后错过了DB::beginTransaction()

如果您不包括该内容,则不会丢失任何内容,因为交易会丢失(与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被执行。