<?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');
}