Laravel |更新登录用户 - save() 与 update()
Laravel | Updating the Logged-In User - save() vs update()
我想更新登录用户的字段。
在我的控制器中,这有效:
Auth::user()->the_field = $theField;
Auth::user()->save();
这不是:
Auth::user()->update(['the_field' => $theField]);
我希望它能工作,因为类似的代码(例如更新订单)工作正常。类似于:
$order->update(['order_status' => OrderStatus::ORDER_COMPLETED]);
为什么它不起作用?我做错了什么吗?
Auth::user()
包含用户信息的模型集合。那是因为更新方法对它不起作用,更新方法只对模型实例有效。
例如看这段代码:
//////////////////////////////////////////////////////////////////
first way
//////////////////////////////////////////////////////////////////
$user = User::where('id',$id)->first();
// if you dd $user here you will get something like this
User {#4989 ▼
#fillable: array:36 [▶]
#dates: array:3 [▶]
#hidden: array:2 [▶]
#collection: null
#primaryKey: "_id"
#parentRelation: null
#connection: "mongodb"
#table: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [▶]
#original: array:20 [▶]
#casts: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:2 [▶]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
-roleClass: null
-permissionClass: null
}
//and first() method ends other queries because it returns collection not Builder class
//this doesn't work
$user->update(['the_field' => $theField]);
//but this will work
$user->the_field = $theField;
$user->save();
//////////////////////////////////////////////////////////////////
second way
//////////////////////////////////////////////////////////////////
$user = User::find($id);
//this will work
$user->update(['the_field' => $theField]);
//and this will work too
$user->the_field = $theField;
$user->save();
使用创建或更新方法时,您必须在 $fillable 属性 用户模型中添加要更新的字段。基于 Laravel Documentation
protected $fillable = [
'the_field'
];
我想更新登录用户的字段。
在我的控制器中,这有效:
Auth::user()->the_field = $theField;
Auth::user()->save();
这不是:
Auth::user()->update(['the_field' => $theField]);
我希望它能工作,因为类似的代码(例如更新订单)工作正常。类似于:
$order->update(['order_status' => OrderStatus::ORDER_COMPLETED]);
为什么它不起作用?我做错了什么吗?
Auth::user()
包含用户信息的模型集合。那是因为更新方法对它不起作用,更新方法只对模型实例有效。
例如看这段代码:
//////////////////////////////////////////////////////////////////
first way
//////////////////////////////////////////////////////////////////
$user = User::where('id',$id)->first();
// if you dd $user here you will get something like this
User {#4989 ▼
#fillable: array:36 [▶]
#dates: array:3 [▶]
#hidden: array:2 [▶]
#collection: null
#primaryKey: "_id"
#parentRelation: null
#connection: "mongodb"
#table: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [▶]
#original: array:20 [▶]
#casts: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:2 [▶]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
-roleClass: null
-permissionClass: null
}
//and first() method ends other queries because it returns collection not Builder class
//this doesn't work
$user->update(['the_field' => $theField]);
//but this will work
$user->the_field = $theField;
$user->save();
//////////////////////////////////////////////////////////////////
second way
//////////////////////////////////////////////////////////////////
$user = User::find($id);
//this will work
$user->update(['the_field' => $theField]);
//and this will work too
$user->the_field = $theField;
$user->save();
使用创建或更新方法时,您必须在 $fillable 属性 用户模型中添加要更新的字段。基于 Laravel Documentation
protected $fillable = [
'the_field'
];