From 9c37542e6f3acba1e4ab08d2db05a367151790b9 Mon Sep 17 00:00:00 2001 From: kusowl Date: Wed, 14 Jan 2026 13:36:36 +0530 Subject: [PATCH] feature(broker profile): broker can edit their profile --- .../Broker/BrokerProfileController.php | 37 +++++++++++++++++-- .../Requests/StoreBrokerProfileRequest.php | 33 +++++++++++++++++ app/Models/User.php | 2 +- .../dashboards/broker/profile/edit.blade.php | 33 +++++++++++++++++ .../dashboards/broker/profile/show.blade.php | 14 ++++++- 5 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 app/Http/Requests/StoreBrokerProfileRequest.php create mode 100644 resources/views/dashboards/broker/profile/edit.blade.php diff --git a/app/Http/Controllers/Broker/BrokerProfileController.php b/app/Http/Controllers/Broker/BrokerProfileController.php index cf74a8b..8379b21 100644 --- a/app/Http/Controllers/Broker/BrokerProfileController.php +++ b/app/Http/Controllers/Broker/BrokerProfileController.php @@ -3,9 +3,12 @@ namespace App\Http\Controllers\Broker; use App\Http\Controllers\Controller; +use App\Http\Requests\StoreBrokerProfileRequest; use App\Models\Broker; use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; class BrokerProfileController extends Controller @@ -39,23 +42,49 @@ public function show(User $profile) ->with('initials', $initials) ->with('verified', $broker->verified) ->with('location', $broker->location) + ->with('bio', $broker->bio) ->with('phone', $broker->phone); } /** * Show the form for editing the specified resource. */ - public function edit(string $id) + public function edit(User $profile) { - // + return view('dashboards.broker.profile.edit') + ->with('profile', $profile) + ->with('broker', $profile->type); } /** * Update the specified resource in storage. */ - public function update(Request $request, string $id) + public function update(StoreBrokerProfileRequest $request, User $profile) { - // + /** + * Separate the user fields from the broker fields + */ + $userFields = ['name', 'email']; + $data = collect($request->validated()); + $userData = $data->only($userFields)->toArray(); + $brokerData = $data->except($userFields)->toArray(); + + try { + DB::transaction(function () use ($profile, $userData, $brokerData) { + $profile->update($userData); + $broker = $profile->type; + + Broker::unguard(); + $broker->update($brokerData); + Broker::reguard(); + }); + + return to_route('broker.profile.show', $profile) + ->with('success', 'Profile updated successfully.'); + } catch (\Throwable $e) { + Log::error('Broker Profile Update Failed: '.$e->getMessage(), $e->getTrace()); + return back()->withInput()->with('error', 'Something went wrong.'); + } } /** diff --git a/app/Http/Requests/StoreBrokerProfileRequest.php b/app/Http/Requests/StoreBrokerProfileRequest.php new file mode 100644 index 0000000..85f86fc --- /dev/null +++ b/app/Http/Requests/StoreBrokerProfileRequest.php @@ -0,0 +1,33 @@ +user()->isBroker(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string|min:3|max:255', + 'bio' => 'required|string|min:10|max:255', + 'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($this->user()->id)], + 'phone' => 'required|string|min:10|max:255', + 'location' => 'required|string|min:3|max:255' + ]; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index a33dccb..3abaf67 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -54,7 +54,7 @@ protected function casts(): array public function isBroker(): bool { - return $this->role === UserTypes::Broker->value; + return $this->type instanceof Broker; } public function deals(): HasMany diff --git a/resources/views/dashboards/broker/profile/edit.blade.php b/resources/views/dashboards/broker/profile/edit.blade.php new file mode 100644 index 0000000..63414ba --- /dev/null +++ b/resources/views/dashboards/broker/profile/edit.blade.php @@ -0,0 +1,33 @@ + + +
+ + +

Profile Information

+
+ @csrf + @method('PATCH') + + + + + + + + + + + +
+ Update + Cancel +
+ +
+
+
diff --git a/resources/views/dashboards/broker/profile/show.blade.php b/resources/views/dashboards/broker/profile/show.blade.php index d069a59..f02c426 100644 --- a/resources/views/dashboards/broker/profile/show.blade.php +++ b/resources/views/dashboards/broker/profile/show.blade.php @@ -11,6 +11,18 @@ + + +
+ @session('success') + {{$value}} + @endsession + + @session('error') + {{$value}} + @endsession +
+
@@ -33,7 +45,7 @@
-
+

{{$bio ?? 'Bio is empty'}}