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/Certificate/Add.php
<?php

namespace App\Livewire\Certificate;

use Livewire\Component;
use Livewire\WithFileUploads;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http; 
use Illuminate\Support\Facades\Session;

class Add extends Component
{
    use WithFileUploads;

    public $title;
    public $category_id;
    public $cat_id;
    public $user_id;
    public $years;
    public $year_obtained;
    public $document;
    public $subcategories = [];

    protected $rules = [
        'user_id' => 'required|exists:shops,user_id',
        'category_id' => 'required|exists:categories,id',
        'title' => 'required|string|max:255',
        'year_obtained' => 'nullable|digits:4',
        'document' => 'required|file|mimes:jpeg,jpg,png,pdf|max:2048', // Max size in kilobytes (2MB in this example)
    ];

    public function mount()
    {
        $shop = Session::get('shop');
        $this->category_id = $shop['category_id'];


        $body = ['seller_category_id' => $this->category_id];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->get(ApiEndpoints::getSellerCategory());
        $this->subcategories =  $response->json()['data'][0]['children'];

        $this->user_id = $shop['user_id'];

        $currentYear = now()->year;
        $this->years = range($currentYear - 20, $currentYear);
    }

    public function addShopCertificate()
    {
        try {
            $this->validate();
            $document_url = null;
            $document_public_url = null;

            if ($this->document) {
                $uploadedFile = $this->document->getRealPath();
                $uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'sellerCertificates']);
                $document_url = $uploadResult->getSecurePath();
                $document_public_url = $uploadResult->getPublicId();
            }

            $body = [
                'user_id' => $this->user_id,
                'category_id' => $this->cat_id,
                'title' => $this->title,
                'year_obtained' => $this->year_obtained,
                'document' => $document_url,
                'document_public_url' => $document_public_url,
            ];
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->post(ApiEndpoints::saveCertificate());

            if ($response->successful()) {
                $certificate = $response->json()['data'];
                Session::put('certificate', $certificate);
                $certificate = Session::get('certificate');
                Session::flash('success', 'Certificate successfully added.');
                return redirect()->to('/dashboard-certificates');
            } else {
                Log::error('Failed to add a new service ' . $response->json()['data']);
                $this->addError('document_url', $response->json('data'));
            }
        } catch (\Throwable $th) {
            $this->addError('document_url', $th->getMessage());
        }
    }
    public function render()
    {

        return view('livewire.certificate.add');
    }
}