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

ammar_ahmed's avatar

The image is not uploading in livewire and breeze.

I install breeze with volt livewire for my project. I add image filed for user to upload there profile picture but when i submit the form and get the ErrorException Trying to access array offset on null.

<?php

use App\Models\User;
use Livewire\Volt\Component;
use Livewire\WithFileUploads;
use Livewire\Attributes\Layout;
use Illuminate\Validation\Rules;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;

new #[Layout('layouts.guest')] class extends Component {
    public string $name = '';
    public string $email = '';
    public string $password = '';
    public string $password_confirmation = '';
    public $image;

    /**
     * Handle an incoming registration request.
     */
    use WithFileUploads;

    public function register(): void
    {
        $validated = $this->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
            'password' => ['required', 'string', 'confirmed', Rules\Password::defaults()],
            'image' => ['required', 'image'],
        ]);
        if ($this->image) {
            $validated['image'] = $this->image->store('image');
        }
        $validated['password'] = Hash::make($validated['password']);

        event(new Registered(($user = User::create($validated))));

        Auth::login($user);

        $this->redirect(route('dashboard', absolute: false), navigate: true);
    }
}; ?>

<div>
    <form wire:submit="register" enctype="multipart/form-data">
        <!-- Name -->
        <div>
            <x-input-label for="name" :value="__('Name')" />
            <x-text-input wire:model="name" id="name" class="block mt-1 w-full" type="text" name="name" required
                autofocus autocomplete="name" />
            <x-input-error :messages="$errors->get('name')" class="mt-2" />
        </div>

        <!-- Email Address -->
        <div class="mt-4">
            <x-input-label for="email" :value="__('Email')" />
            <x-text-input wire:model="email" id="email" class="block mt-1 w-full" type="email" name="email"
                required autocomplete="username" />
            <x-input-error :messages="$errors->get('email')" class="mt-2" />
        </div>

        <!-- Password -->
        <div class="mt-4">
            <x-input-label for="password" :value="__('Password')" />

            <x-text-input wire:model="password" id="password" class="block mt-1 w-full" type="password" name="password"
                required autocomplete="new-password" />

            <x-input-error :messages="$errors->get('password')" class="mt-2" />
        </div>

        <!-- Confirm Password -->
        <div class="mt-4">
            <x-input-label for="password_confirmation" :value="__('Confirm Password')" />

            <x-text-input wire:model="password_confirmation" id="password_confirmation" class="block mt-1 w-full"
                type="password" name="password_confirmation" required autocomplete="new-password" />

            <x-input-error :messages="$errors->get('password_confirmation')" class="mt-2" />
        </div>

        <div class="mt-4">
            <x-input-label for="image" :value="__('Logo')" />

            <x-text-input wire:model="image" id="image" class="block mt-1 w-full" type="file" name="image" />

            <x-input-error :messages="$errors->get('image')" class="mt-2" />
        </div>

        <div class="flex items-center justify-end mt-4">
            <a class="underline text-sm text-gray-600 hover:text-gray-900 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                href="{{ route('login') }}" wire:navigate>
                {{ __('Already registered?') }}
            </a>

            <x-primary-button class="ms-4">
                {{ __('Register') }}
            </x-primary-button>
        </div>
    </form>
</div>
0 likes
16 replies
Tray2's avatar

Try changing this line

<x-text-input wire:model="image" id="image" class="block mt-1 w-full" type="file" name="image" />

To

 <input type="file" wire:model="image"  class="block mt-1 w-full"/>
Tray2's avatar

@Raja_Ammar I'm guessing you get a 302 back to the create page.

So check your validations.

ammar_ahmed's avatar

@Tray2

<br />
<b>Warning</b>:  File upload error - unable to create a temporary file in <b>Unknown</b> on line <b>0</b><br />
{"message":"The files.0 failed to upload.","errors":{"files.0":["The files.0 failed to upload."]}}

when submit the form in the network tap i get the this response and the status code is 422

Tray2's avatar

@Raja_Ammar Have you checked so you have permissions to write to the tmp directory?

Tray2's avatar

@Raja_Ammar No, on the file system. Every file and directory has permissions set to them, and all files belongs to a user and group. That way a file created by one user can depending on the permissions prevent other users from access to it. My guess here is that the www-user doesn't have write access to the temp directory.

https://www.redhat.com/sysadmin/linux-file-permissions-explained

What OS are you using?

Please or to participate in this conversation.