92 lines
2.7 KiB
PHP
92 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Broker;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreBrokerProfileRequest;
|
|
use App\Models\Broker;
|
|
use App\Models\User;
|
|
use App\Services\ProfileInitialsService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
class BrokerProfileController extends Controller
|
|
{
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(User $profile, ProfileInitialsService $service)
|
|
{
|
|
// Get the broker profile
|
|
$broker = $profile->type;
|
|
|
|
// TODO: move this to middleware
|
|
if (! $broker instanceof Broker) {
|
|
abort(403, 'This user is not a broker.');
|
|
}
|
|
|
|
$initials = $service->create($profile->name);
|
|
|
|
return view('dashboards.broker.profile.show')
|
|
->with('name', $profile->name)
|
|
->with('joinDate', $profile->created_at->format('F Y'))
|
|
->with('email', $profile->email)
|
|
->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(User $profile)
|
|
{
|
|
return view('dashboards.broker.profile.edit')
|
|
->with('profile', $profile)
|
|
->with('broker', $profile->type);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
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.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|