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

Kikismedia's avatar

Call to a member function storeAs() on null

trying to upload avatar for user getting thisError Call to a member function storeAs() on null

 use WithFileUploads;

 protected $rules = [

        'user.avatar' => 'image|max:1032',
       // must add addition fields here
    ];


    public function save()
    {
        // dd($this->user);
        $this->validate();
        $this->avatar->storeAs('avatar', 'public');
        $this->user->save();

        $this->updateMode = false;
        session()->flash('message', 'Users Updated Successfully.'); // this works?


        $this->reset();
    }
0 likes
6 replies
anilkumarthakur60's avatar

check your form you might me be missing ``` enctype="multipart/form-data"

 <form action="" enctype="multipart/form-data" method="post">
    @csrf
    ..
    </form>
Snapey's avatar

I don't think you can have the uploaded image as part of the user object?

When you save the user, you will get an error that the model cannot store the uploaded file object.

Use a separate attribute for the image

AlexElementarteilchen's avatar

Hi,

from this 'user.avatar' => 'image|max:1032', I am guessing that you use the User model in the livewire component like this:

public User $user;

If that is the case then I think it should be

$this->user->avatar->storeAs('avatar', 'public');

instead of just

$this->avatar->storeAs('avatar', 'public');

because that is what PHP is complaining about with Call to a member function storeAs() on null

Same as Snapey I would also recommend to use a separate attribute:

public $avatar;
AlexElementarteilchen's avatar

@Kikismedia check out the example on the livewire site again: https://laravel-livewire.com/docs/2.x/file-uploads.

In the example they use public $photo; and then in the save() method call

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

to store the uploaded file.

In your code you have

$this->avatar->storeAs('avatar', 'public');

but it seems that avatar is not declared as a variable. That is why you get the error message.

I would try to declare public User $user in the class and then change the save method to:

$this->user->avatar->storeAs('avatar', 'public');

Also make sure to also check the HTML form and use <input type="file" wire:model="user.avatar">

Hope this helps!

Please or to participate in this conversation.