43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Client;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Client\ProfileUpdateRequest;
|
|
use App\Models\Medium;
|
|
use App\Services\FileStorageService;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ProfileController extends Controller
|
|
{
|
|
protected FileStorageService $fileStorageService;
|
|
|
|
public function __construct(FileStorageService $fileStorageService)
|
|
{
|
|
$this->fileStorageService = $fileStorageService;
|
|
}
|
|
|
|
public function update(ProfileUpdateRequest $request)
|
|
{
|
|
$user = Auth::user();
|
|
$data = $request->safe()->except('avatar');
|
|
|
|
if ($request->hasFile('avatar')) {
|
|
if (!empty($user->profile_url)) {
|
|
if ($medium = Medium::where('url', $user->profile_url)->first()) {
|
|
$this->fileStorageService->deleteMedium($medium);
|
|
}
|
|
}
|
|
|
|
$medium = $this->fileStorageService->storeFile($request->file('avatar'), 'public', 'profile');
|
|
$data['profile_url'] = $medium->url;
|
|
}
|
|
|
|
$user->update($data);
|
|
|
|
session()->flash('success', 'حساب کاربری شما با موفقیت ویرایش شد.');
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|