Laravel Livewire File Upload return Null

Viewed 256

I am having an issue with livewire on uploading files. I am using a very basic example to achieve my goal but the issue is that it returns null on Submit.

Here is my Livewire controller Code:

class UploadPhoto extends Component

{

    use WithFileUploads;

 

    public $photo;

 

    public function save()

    {

        dd($this->photo); //returns null

        $this->validate([

            'photo' => 'image|max:1024', // 1MB Max

        ]);

 

        $this->photo->store('photos');

    }

}

        

            

<form wire:submit.prevent="save">

    <input type="file" wire:model="photo">

 

    @error('photo') <span class="error">{{ $message }}</span> @enderror

 

    <button type="submit">Save Photo</button>

</form>
2 Answers

you sure use withFileUploads?

use Livewire\WithFileUploads;

So, as I said before, I also had the same issue a few weeks ago.

I had a very basic Livewire Controller to Store an image, but no matter what I did the input kept returning a NULL value or kept saying that the image input was REQUIRED even if the image was selected.

But, if I used a regular Controller (no Livewire) everything worked perfectly somehow.

After wasting many hours, I still launched my site on production and noticed that everything worked like a charm in there.

So I kept wondering: Why is it NOT WORKING in my LOCAL environment?

Everything was up-to-date !

I was also running my project locally and had been working with a Hot Reload served on the Port :3000 (http://localhost:3000).

As the issue was driving me crazy, I stopped the Hot Reload and continued testing everything on the main served hostname (localhost).

And magically... everything worked.

I still don't understand why Livewire ain't working properly with a Hot Reload. I mean, everything works like it should except for the input type="file" to store an image.

Hope this helps a bit!

Related