HEX
Server: LiteSpeed
System: Linux php-prod-3.spaceapp.ru 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: xnsbl7462 (1008)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /home/marketing.cfbon.ru/public_html/app/Observers/GalleryObserver.php
<?php

namespace App\Observers;

use App\Http\Requests\Site\GalleryRequest;
use App\Models\Gallery;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GalleryObserver
{
    protected Request $request;
    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    public function created(Gallery $gallery): void
    {
        $this->saveImage($gallery);
    }
    public function updated(Gallery $gallery): void
    {
        if ($this->request->hasFile('img')) {
            $this->deleteOldImage($gallery);
            $this->saveImage($gallery);
        }
    }
    public function deleted(Gallery $gallery): void
    {
        $this->deleteOldImage($gallery);
    }
    private function saveImage(Gallery $gallery): void
    {
        if (! request()->hasFile('img')) {
            return;
        }

        $file = request()->file('img');
        $path = $file->store('gallery', 'public');
        $gallery->forceFill(['img' => $path])->saveQuietly();
    }
    private function deleteOldImage(Gallery $gallery): void
    {
        if ($oldPath = $gallery->getOriginal('img')) {
            Storage::disk('public')->delete($oldPath);
        }
    }
}