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/html/app/Livewire/Shop/Update.php
<?php

namespace App\Livewire\Shop;

use App\Models\User;
use Livewire\Component;
use Illuminate\Support\Str;
use Livewire\WithFileUploads;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;

class Update extends Component
{
    use WithFileUploads;

    public $category_id;
    public $user_id;
    public $name;
    public $description;
    public $address;
    public $profile_photo_url;
    public $workshop_photo_urls = [];
    public $city_id;
    public $country_id = "2";
    public $seller_plan_id = "1";
    public $selectedState = null;

    public $states = [];
    public $cities = [];
    public $countries = [];
    public $categories = [];
    public $plans = [];

    public function mount()
    {
        $this->states = DB::table('states')->orderBy('name', 'asc')->get();
        $this->categories = DB::table('categories')->whereNull('parent_id')->orderBy('name', 'asc')->get();

        $user = Session::get('user');
        $this->user_id = $user['id'];

        $this->updatedSelectedState($this->selectedState);
    }

    public function updatedSelectedState()
    {
        $this->cities = DB::table('cities')->where('state_id', $this->selectedState)->orderBy('name', 'asc')->get();
    }


    public function updateShop()
    {
        try {
            $this->validate();
            if ($this->profile_photo_url) {
                $uploadedFile = $this->profile_photo_url->getRealPath();
                $uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'sellerProfilePhoto']);
                $profile_photo_url = $uploadResult->getSecurePath();
                $profile_photo_public_id = $uploadResult->getPublicId();
            }

            $workshop_photo_urls = [];
            $workshop_photo_public_ids = [];

            if ($this->workshop_photo_urls) {
                foreach ($this->workshop_photo_urls as $photo) {
                    $uploadedPhoto = $photo->getRealPath();
                    $uploadResult = cloudinary()->upload($uploadedPhoto, ['folder' => 'workshopPhotos']);
                    $workshop_photo_urls[] = $uploadResult->getSecurePath();
                    $workshop_photo_public_ids[] = $uploadResult->getPublicId();
                }
            }

            User::where('user_id', $this->user_id)->update([
                'profile_photo_url' => $profile_photo_url,
                'profile_photo_public_id' => $profile_photo_public_id,
            ]);
            $body = [
                'user_id' => $this->user_id,
                'category_id' => $this->category_id,
                'profile_photo_url' => $profile_photo_url,
                'profile_photo_public_id' => $profile_photo_public_id,
                'workshop_photo_urls' => $workshop_photo_urls,
                'workshop_photo_public_ids' => $workshop_photo_public_ids,
                'name' => ucwords($this->name),
                'slug' => Str::slug($this->name),
                'description' => ucfirst($this->description),
                'address' => $this->address,
                'country_id' => $this->country_id,
                'state_id' => $this->selectedState,
                'city_id' => $this->city_id,
                'seller_plans_id' => $this->seller_plan_id,
            ];

            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->post(ApiEndpoints::updateShop());

            if ($response->successful()) {
                $shop = $response->json()['data'];
                Session::put('shop', $shop);
                $shop = Session::get('shop');
                // Session::flash('success', 'Shop Info updated successfully.');
                $this->addError('workshop_photo_urls', 'Shop Info updated successfully');
                // return redirect()->to('/dashboard-gig');
            } else {
                $this->addError('workshop_photo_urls', $response->json()['data']);
            } 
        } catch (\Throwable $th) {
            $this->addError('workshop_photo_urls', $th->getMessage());
        }
    }
    public function render()
    {

       return view('livewire.shop.show');
    }
}