File: /var/www/admin.fixgini.com/app/Livewire/Profile/UpdatePhoto.php
<?php
namespace App\Livewire\Profile;
use App\Models\Shop;
use Livewire\Component;
use Livewire\WithFileUploads;
class UpdatePhoto extends Component
{
use WithFileUploads;
public $profile_photo;
public $user_id;
public $successMessage;
protected $rules = [
"profile_photo" => "required|image",
];
public function mount()
{
$shop = Shop::where('user_id', auth()->user()->id)->first();
$this->user_id = auth()->user()->id;
$this->profile_photo = $shop->profile_photo ?? '';
}
public function savePhoto()
{
$this->validate();
try {
$shop = Shop::where('user_id', $this->user_id)->first();
if ($shop) {
if ($this->profile_photo) {
$uploadedFile = $this->profile_photo->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile);
$uploadedFileUrl = $uploadResult->getSecurePath();
$publicId = $uploadResult->getPublicId();
}
$shop->update([
'profile_photo' => $uploadedFileUrl ?? 'icon.png',
'profile_photo_public_id' => $publicId ?? 'icon.png',
]);
$this->addError('profile_photo', 'Profile photo updated successfully.');
} else {
$this->addError('profile_photo', 'You do not have shop');
}
} catch (\Throwable $th) {
session()->flash('profile_photo', $th->getMessage());
}
}
public function render()
{
return view('livewire.profile.update-photo');
}
}