From b54f9f052781d21dc1aa5a9407bfb6f4ee5a77d6 Mon Sep 17 00:00:00 2001 From: adonis Date: Wed, 2 Sep 2026 11:18:59 +0330 Subject: [PATCH] fix(customer): save only profile fields on admin PUT The list edit dumped the whole table row into update(); whitelist form and accounting fields, and skip hashing an empty password. Co-authored-by: Cursor --- .../Controllers/Admin/CustomerController.php | 65 ++++++++++++++----- app/Models/Customer.php | 3 + 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Admin/CustomerController.php b/app/Http/Controllers/Admin/CustomerController.php index d83591b..244bad4 100644 --- a/app/Http/Controllers/Admin/CustomerController.php +++ b/app/Http/Controllers/Admin/CustomerController.php @@ -129,17 +129,39 @@ class CustomerController extends SuperController public function updateAdmin(Request $request , $id) { - $data = $request->all(); - if (isset($data['mobile']) && !is_null($data['mobile'])){ + $id = (int) $id; + $data = $request->only([ + 'fname', + 'lname', + 'mobile', + 'password', + 'birth_date', + 'phone_number', + 'province', + 'city', + 'n_code', + 'add_user_limit', + 'referral_to_id', + 'pargar_account_id', + 'moshtari_code', + 'profit_amount_by_lvl', + 'gold', + ]); + + if (array_key_exists('password', $data) && trim((string) ($data['password'] ?? '')) === '') { + unset($data['password']); + } + + if (isset($data['mobile']) && ! is_null($data['mobile']) && $data['mobile'] !== '') { $data['mobile'] = Customer::normalizeMobile($data['mobile']); - Customer::assertMobileUnique($data['mobile'], (int) $id); + Customer::assertMobileUnique($data['mobile'], $id); } if (array_key_exists('pargar_account_id', $data)) { $data['pargar_account_id'] = $this->normalizeAndAssertUniqueAccountingCode( $data['pargar_account_id'], 'pargar_account_id', - (int) $id + $id ); } @@ -147,21 +169,34 @@ class CustomerController extends SuperController $data['moshtari_code'] = $this->normalizeAndAssertUniqueAccountingCode( $data['moshtari_code'], 'moshtari_code', - (int) $id + $id ); } - if (isset($data['gold'])){ - $user = Customer::find($id); - if ($user) { - $user->gold = $data['gold']; - $user->save(); - $user["g"]=true; - } - } - $user["u"] = Customer::findOrFail($id)->update($data); - return response()->json($user); + $customer = Customer::findOrFail($id); + if (array_key_exists('referral_to_id', $data) && filled($customer->referral_to_id)) { + unset($data['referral_to_id']); + } + + if (isset($data['fname'], $data['lname'])) { + $data['name'] = trim($data['fname'].' '.$data['lname']); + } + + $goldUpdated = false; + if (array_key_exists('gold', $data) && $data['gold'] !== null && $data['gold'] !== '') { + $customer->gold = $data['gold']; + $customer->save(); + $goldUpdated = true; + } + unset($data['gold']); + + $updated = $data === [] ? false : $customer->update($data); + + return response()->json([ + 'g' => $goldUpdated, + 'u' => $updated, + ]); } /** diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 866c354..0c9a06a 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -130,6 +130,9 @@ class Customer extends Authenticatable implements JWTSubject // hash password public function setPasswordAttribute($value){ + if ($value === null || $value === '') { + return; + } $this->attributes['password'] = Hash::make($value); }