简体   繁体   中英

I am trying to upload a profile image within my Laravel 8 application. I however keep getting "Attempt to read property "image" on array" error

I am trying to save/upload a profile image in my Laravel 8 application. I am using the stock controller "UpdateUserProfileInformation.update" in Laravel Fortify I however keep getting this error

ErrorException Attempt to read property "image" on array

View

...
<form method="POST" action="{{ route('user-profile-information.update') }}">
                    @csrf
                    @method('PUT')
...
<input class="file-upload d-none" type="file" accept="image/*" id="image" name="image" />
...

Controller

public function update($user, array $input)
    {
        ...
            $fileName = time().'.'.$input['image'];
            $input->image->move(public_path('uploads'), $fileName);
    ...

Obviously my issue is that i am trying to save an array, but I can't figure out how to isolate this single file.

when working with files in form you should add an enctype

<form enctype="multipart/form-data" method="post" action="">

You need to add enctype="multipart/form-data" in form tag when working with file

Replace

<form method="POST" action="{{ route('user-profile-information.update') }}">

With

<form method="POST" action="{{ route('user-profile-information.update') }}" enctype="multipart/form-data">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM