From 2abdd2b29069fa9369ebc9bcfcb62b51784e6799 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Oct 2019 18:18:17 +0800 Subject: [PATCH] update --- app/Http/Controllers/Admin/UserController.php | 24 ++++++++++++------- app/Models/User.php | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 8507a5e4..d948bdeb 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -26,22 +26,28 @@ class UserController extends Controller } public function update (UserUpdate $request) { + $fetchData = $request->only([ + 'email', + 'password', + 'transfer_enable', + 'expired_at', + 'banned', + 'is_admin' + ]); $user = User::find($request->input('id')); if (!$user) { abort(500, '用户不存在'); } - if (User::where('email', $request->input('email'))->first() && $user->email !== $request->input('email')) { + if (User::where('email', $fetchData['email'])->first() && $user->email !== $fetchData['email']) { abort(500, '邮箱已被使用'); } - $user->email = $request->input('email'); - if ($request->input('password')) { - $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT); + if ($fetchData['password']) { + $fetchData['password'] = password_hash($fetchData['password'], PASSWORD_DEFAULT); + } else { + unset($fetchData['password']); } - $user->transfer_enable = $request->input('transfer_enable') * 1073741824; - $user->expired_at = $request->input('expired_at'); - $user->banned = $request->input('banned'); - $user->is_admin = $request->input('is_admin'); - if (!$user->save()) { + $fetchData['transfer_enable'] = $fetchData['transfer_enable'] * 1073741824; + if (!$user->update($fetchData)) { abort(500, '保存失败'); } return response([ diff --git a/app/Models/User.php b/app/Models/User.php index 8137761f..cb118056 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,4 +8,5 @@ class User extends Model { protected $table = 'v2_user'; protected $dateFormat = 'U'; + protected $guarded = ['id']; }