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/console.fixgini.com/app/Http/Controllers/Admin/HeroController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Models\Hero;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class HeroController extends Controller
{
    public function get()
    {
        $heros = Hero::InRandomOrder()->limit(4)->get();
        return response()->json(['status' => 'success', 'message' => 'Fetch successfully', 'data' => $heros]);
    }
    
    public function list()
    {
        $heros = Hero::InRandomOrder()->limit(4)->get();
        return response()->json(['status' => 'success', 'message' => 'Fetch successfully', 'data' => $heros]);
    }

    public function store(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'title' => 'required|unique:heroes,title',
                'subtitle' => 'nullable|unique:heroes,subtitle',
                'banner_url' => 'sometimes|image|mimes:jpeg,png,jpg,gif|max:1025',
            ]);

            if ($request->hasFile('banner_url')) {
                $uploadedFile = $request->file('banner_url')->getRealPath();
                $uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'heroBanners']);
                $uploadedFileUrl = $uploadResult->getSecurePath();
                $publicId = $uploadResult->getPublicId();
            }

            $hero = Hero::create([
                'title' => ucwords($validatedData['title']),
                'subtitle' => ucfirst($validatedData['subtitle']),
                'banner_url' => $uploadedFileUrl ?? NULL,
                'banner_public_id' => $publicId ?? NULL,
            ]);
            return response()->json(['status' => 'success', 'message' => 'Hero Banner created successfully', 'hero' => $hero], 200);

        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            
            $validatedData = $request->validate([
                'id' => 'required|exists:heroes,id',
                'title' => 'required',
                'subtitle' => 'nullable',
                'banner_url' => 'sometimes|image|mimes:jpeg,png,jpg,gif|max:1025',
            ]);
            $hero = Hero::findOrFail($id);

            // If icon is uploaded, store it
            if ($request->hasFile('banner_url')) {
                $uploadedFile = $request->file('banner_url')->getRealPath();
                $uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'heroBanners']);
                $uploadedFileUrl = $uploadResult->getSecurePath();
                $publicId = $uploadResult->getPublicId();

                // Delete the old icon file and public ID if they exist
                if ($hero->banner_public_id) {
                    cloudinary()->destroy($hero->banner_public_id);
                }
            } else {
                $uploadedFileUrl = $hero->banner_url;
                $publicId = $hero->banner_public_id;
            }

            $hero->update([
                'title' => ucwords($validatedData['title']),
                'subtitle' => ucfirst($validatedData['subtitle']), 
                'banner_url' => $uploadedFileUrl,
                'banner_public_id' => $publicId,
            ]);

            return response()->json(['status' => 'success', 'message' => 'Hero Banner updated successfully', 'hero' => $hero], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
        }
    }
}