3

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>
5
  • Are you sure you are using Livewire 1.2.0 or higher? Commented Jun 20, 2022 at 14:04
  • Yes i am using 2.0 Commented Jun 20, 2022 at 17:18
  • If you treat Livewire form as a normal form it is wrong, the livewire state is different with Laravel, you can't access livewire properties on form summation Commented Jun 23, 2022 at 7:35
  • Elaborate a bit with example. Because given example is almost same as given on livewire docs Commented Jun 23, 2022 at 8:14
  • IDK why there isn't a fair Answer to this... I've been having the exact same problem for a few days now. Have you resolved your issue? Commented Sep 4, 2022 at 14:40

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Stopping hot reload fixes things. thanks a lot bro. you saved me.
0

you sure use withFileUploads?

use Livewire\WithFileUploads;

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Your answer "as is" is not helpful, I think you could elaborate a little more on the topic or link to another answer in order to help the OP
0

Your code block seems to be ok but there is one missing tag to prevent update element. For that we use wire:ignore.self. wire:ignore.self will inform livewire to don't update this element or it's children".

Here is the correct code for your example:

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

    }

}

        

            
<div wire:ignore.self>
  <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>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.