Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Selvatico's avatar

Spatie media library does not associate media with model

Hello guys, I have been fumbling around this issue for a while and I have not made any tangible progress. I am using the spatie media library component to upload files using livewire. The file is being uploaded and save as a temporary upload to the database but by the time I am calling the following code, variable 'myUpload' is null:

dump($this->myUpload);//null
$model->addFromMediaLibraryRequest($this->myUpload)->toMediaCollection('myupload');

what could be the issue?

0 likes
8 replies
vincent15000's avatar

Have you set the correct configuration of your form to upload files ?

Sinnbeck's avatar

Show some more code. It's impossible to debug based on one line of code

1 like
Selvatico's avatar

@Sinnbeck Here is snippet of my component code:

// OrderShow.php

...
class OrderShow extends Component
{
    use LivewireAlert, WithMedia;
	public $mediaComponentNames = ['myUpload'];
	public $myUpload;
	...
	public function mount($order)
    {
		$this->order = $order;
	}
	...
	public function submit()
    {
		//dump($this->myUpload);//this return null
		$this->order->addFromMediaLibraryRequest($this->myUpload)
->toMediaCollection('myupload');
        $this->clearMedia();
        $this->alert('success', 'Your files have been uploaded successfully!', ['toast' => false, 'position' =>'center']) //this toast gets shown
    }
	...
}

// order-show.blade.php

<div class="container-xxl">
    <form wire:submit.prevent="submit">
        <x-media-library-attachment name="myUpload" />
        <button type="submit">Submit</button>
    </form>
</div>

NB: After I select a file the component makes a server round trip and saves the file to the database as TemporaryUpload model type.

1 like
Selvatico's avatar
Selvatico
OP
Best Answer
Level 3

@vincent15000 I have tried adding enctype but still not working. Before it was working without enctype and then suddenly it just won't work and tracing the exact point when it started failing is too expensive so I have just resorted to use filepond in place of spatie media-library-pro.

Please or to participate in this conversation.