GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
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');
    }
}