【问题标题】:Laravel 5.4 User model not updatingLaravel 5.4 用户模型未更新
【发布时间】:2018-07-10 03:59:53
【问题描述】:

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

我有以下 \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”列。

对此的任何建议表示赞赏,对此我失去了理智。

【问题讨论】:

    标签: model controller laravel-5.4


    【解决方案1】:

    您在DB::beginTransaction() 之后缺少DB::commit();如果你不包含它,则不会“保存”任何内容,因为事务会丢失(这与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

    【讨论】:

    • 谢谢你,这让我发疯了,不知道我怎么没看到。
    • 不用担心;这是一件容易被忽视的事情。很高兴你让它工作了,干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2018-01-27
    • 2017-10-25
    • 2017-08-10
    • 2017-09-27
    相关资源
    最近更新 更多