PHP code example of jhonoryza / laravel-fileupload-component

1. Go to this page and download the library: Download jhonoryza/laravel-fileupload-component library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jhonoryza / laravel-fileupload-component example snippets


use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Jhonoryza\Component\FileUpload\Traits\InteractsWithFileUpload;

class Setting extends Model implements HasMedia
{
    use InteractsWithMedia;
    use InteractsWithFileUpload;

    public function registerMediaCollections() : void
    {
        $this->addMediaCollection('settings');
    }
}

   /**
    * property to store multiple images
    */
    public $images = [];

   /**
    * listener when there is onFileReplace event from the component
    */
   #[On('images:onFileReplace')]
   public function replaceImages(array $images): void
   {
       $this->images = $images;
   }

   /**
    * listener when there is onFileAdded event from the component
    */
   #[On('images:onFileAdded')]
   public function addToImages(array $file): void
   {
       $this->images[$file['uuid']] = $file;
   }

   /**
    * form save function example, setting is a Model
    * we call syncFileUploadRequest function
    * to save images to media library
    */
    public function save()
    {
        $this->setting
            ->syncFileUploadRequest($this->images)
            ->toMediaCollection('settings');
    }

    <livewire:file-upload-component
        name="images"
        label="Image"
        :model="$setting"
        collection="settings"
        :multiple="true"
    />

    <livewire:file-upload-component
        name="images"
        label="Image"
        :model="$setting"
        collection="settings"
        :multiple="true"
        :canUploadFile="false"
    />

    <button
        x-data="{ disable: false }"
        x-bind:disabled="disable"
        x-bind:style="disable ? 'cursor: not-allowed' : ''"
        x-on:images:temporary-upload-started.window="disable = true"
        x-on:images:temporary-upload-finished.window="disable = false"
        type="submit"
        class="btn btn-primary"
    >
        Update Setting
    </button>

    <button
        x-data="{ disable: false }"
        x-bind:disabled="disable"
        x-bind:style="disable ? 'cursor: not-allowed' : ''"
        x-on:livewire-upload-start.window="disable = true"
        x-on:livewire-upload-error.window="disable = false"
        x-on:livewire-upload-progress.window="disable = true"
        x-on:livewire-upload-finish.window="disable = false"
        type="submit"
        class="btn btn-primary"
    >
        Update Setting
    </button>

   #[On('images:onFileReplace')]
   public function replaceImages(array $images): void
   {
       $this->images = $images;
   }

   #[On('images:onFileAdded')]
   public function addToImages(array $file): void
   {
       $this->images[$file['uuid']] = $file;
   }
bash
php artisan vendor:publish --provider=Jhonoryza\Component\FileUpload\FileUploadServiceProvider