feature(broker profile): broker can edit their profile

This commit is contained in:
kusowl 2026-01-14 13:36:36 +05:30
parent 5faa3086b1
commit 9c37542e6f
5 changed files with 113 additions and 6 deletions

View File

@ -3,9 +3,12 @@
namespace App\Http\Controllers\Broker; namespace App\Http\Controllers\Broker;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\StoreBrokerProfileRequest;
use App\Models\Broker; use App\Models\Broker;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class BrokerProfileController extends Controller class BrokerProfileController extends Controller
@ -39,23 +42,49 @@ public function show(User $profile)
->with('initials', $initials) ->with('initials', $initials)
->with('verified', $broker->verified) ->with('verified', $broker->verified)
->with('location', $broker->location) ->with('location', $broker->location)
->with('bio', $broker->bio)
->with('phone', $broker->phone); ->with('phone', $broker->phone);
} }
/** /**
* Show the form for editing the specified resource. * 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. * 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.');
}
} }
/** /**

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreBrokerProfileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user()->isBroker();
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|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'
];
}
}

View File

@ -54,7 +54,7 @@ protected function casts(): array
public function isBroker(): bool public function isBroker(): bool
{ {
return $this->role === UserTypes::Broker->value; return $this->type instanceof Broker;
} }
public function deals(): HasMany public function deals(): HasMany

View File

@ -0,0 +1,33 @@
<x-layout title="Edit Broker Profile">
<x-dashboard.page-heading
title="Edit Profile"
description="Modify your profile details"
:back-link="route('broker.profile.show', $profile)"
/>
<div class="flex items-center justify-center mt-8">
<x-dashboard.card class="w-8/12">
<h3 class="text-md font-bold">Profile Information</h3>
<form method="post" enctype="multipart/form-data" action="{{route('broker.profile.update', $profile)}}" class="flex flex-col space-y-8 mt-4">
@csrf
@method('PATCH')
<x-ui.input name="name" label="Name" :value="$profile->name" required placeholder="e.g. John Doe"/>
<x-ui.textarea :value="$broker->bio" name="bio" label="Bio" required
placeholder="Describe yourself in detail..."/>
<x-ui.input name="email" label="Email" :value="$profile->email" required placeholder="example@email.com"/>
<x-ui.input name="phone" label="Phone" :value="$broker->phone" required placeholder="+00 00000 00000"/>
<x-ui.input name="location" label="Location" :value="$broker->location" required placeholder="Kolkata, India"/>
<div class="grid grid-cols-12 w-full space-x-4">
<x-ui.button variant="neutral" class="col-span-10">Update</x-ui.button>
<x-ui.button :link="route('broker.profile.show', $profile)" class=" border border-accent-600/20 col-span-2">Cancel</x-ui.button>
</div>
</form>
</x-dashboard.card>
</div>
</x-layout>

View File

@ -11,6 +11,18 @@
</x-ui.button> </x-ui.button>
</x-slot:end> </x-slot:end>
</x-dashboard.page-heading> </x-dashboard.page-heading>
<div class="wrapper mt-6">
@session('success')
<x-ui.alert variant="success">{{$value}}</x-ui.alert>
@endsession
@session('error')
<x-ui.alert variant="error">{{$value}}</x-ui.alert>
@endsession
</div>
<div class="flex items-center justify-center"> <div class="flex items-center justify-center">
<div class="flex items-center justify-center mt-8 w-11/12 md:-w-10/12"> <div class="flex items-center justify-center mt-8 w-11/12 md:-w-10/12">
<x-dashboard.card class="w-full"> <x-dashboard.card class="w-full">
@ -33,7 +45,7 @@
</div> </div>
</div> </div>
<div class=""> <div class="wrap-break-word">
<p>{{$bio ?? 'Bio is empty'}}</p> <p>{{$bio ?? 'Bio is empty'}}</p>
</div> </div>