Spatie MediaLibrary Public Property

338 Views Asked by At

I am using Spatie MediaLibrary to upload documents into the database and right now I face an issue. Spatie uses public property $mediaComponentNames where I have to assign the media name. For example:

public $mediaComponentNames = ['myUpload'];

The problem is I want the "myUpload" to be dynamic. So, I dont assign any value for $mediaComponentNames in public property, but assign the value in public function mount() instead. I got null value when try to upload document using Spatie, it does not return expected output which is the filename etc.

Is there any other way, or suggestions. Thank you so much.

I got null, the expected output should be a list of array with all the files upload using Spatie Medialibrary.

There are 3 files related to this problem, I just show the part of the code that I think necessary only. I already check that there is no issue to upload to temporary, just that it does not show the expected output that I need.

ApplicationData.php

<?php

namespace Leave\Concerns;

use Carbon\Carbon;
use Setting\Models\Document;

trait ApplicationData
   
{
    public $mediaComponentNames;

    public function initMount($code = null, $uuid = null)
    {   
        $this->documents = $this->getDocuments($this->leave_type);

        /* Assign the array key(document item id) for the array of images uploaded in checklist document */
        foreach ($this->documents as $doc) {
            foreach ($doc->items->where('is_active', Status::Active()) as $item) {
                /* Get the id of document item */
                $this->mediaComponentNames[] = "document_items_" . $item->id;
            } 
        }
    }
}

Application.php

<?php

namespace Leave\Concerns;

use Spatie\MediaLibraryPro\Http\Livewire\Concerns\WithMedia;
use Setting\Enums\Status as SettingStatus;

trait Application
{
    use WithMedia;
    use ApplicationData;


    abstract public function init();

    public function submit()
    {   
        $this->documents = $this->getDocuments($this->leave_type);

        /* Assign the array key(document item id) for the array of images uploaded in checklist document */
        foreach ($this->documents as $doc) {
            foreach ($doc->items->where('is_active', SettingStatus::Active()) as $item) {
                $document_items[] = $this->{ 'document_items_' . $item->id };
            } 
        }

        dd($document_items); // it return array of null, it should return array of document uploaded through Spatie MediaLibrary
    }
}
create.blade.php

<x-uploaded-media
    :file="$file"
    name="document_items_{{ $item->id }}"
    collection="document_items.{{ $item->id }}"
    rules="mimes:pdf, svg"
    deleteFunctionName="removeFile"
/>

The expected output is like below where I only upload 2 file for testing purpose, which is SVG file.The expected output I got it because, I assigned it in the public property like below

public $mediaComponentNames = ["document_items_14","document_items_4","document_items_15","document_items_9","document_items_10"];

Expected output

But instead, I got this

Current output

0

There are 0 best solutions below