HTML file input return curly brackets to Laravel Controller

I have a blade.php. file:

<form method="post" class="w-full max-w-lg" action="/staff/create/program" enctype="multipart/form-data">
                 @csrf
                 <div class="flex flex-wrap -mx-3 mb-6">
                   <div class="w-full px-3">
                     <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="thumbnail">
                       Thumbnail
                     </label>
                     <input type="file" id="thumbnail "name="thumbnail">
 
                   </div>
                 </div>

                 <div class="form-group md:flex md:items-center">
                   <div class="md:w-1/3">
                     <label class="col-md-4 control-label" for="submit"></label>
                     <div class="col-md-4">
                       <button id="submit" name="submit" class="shadow bg-teal-400 hover:bg-teal-400 focus:shadow-outline bg-indigo-400 focus:outline-none text-gray-200 hover:bg-indigo-600 hover:text-white font-bold py-2 px-4 rounded" >Submit</button>
                     </div>
                   </div>
                   <div class="md:w-2/3"></div>
                 </div>
</form>

Whenever I upload a file, it returns the curly brackets {}
HTML file input return curly brackets to Laravel Controller

Controller Code:

    public function StaffRegisterProgram(Request $request)
    {
        return $request->all();
    }

I have included the enctype and it should return me the file name, is there anything I miss here?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The output you are getting is not unexpected.

return $request->all(); will not show you the file.

Why are you seeing those curly braces? This is actually a json. $request->all() method returns an array and Laravel response is converting that array to a json automatically to show in a browser. A browser can’t directly show a PHP array.

To see PHP array style output, we can use:

dd($request->all()) Still, this will show you the same thing just in PHP style instead of not-styled json.

Now, to see if the file exists in request or not you can use:

dd($request->hasFile('thumbnail'))

If this statement is returning true that your file exists in the request which is what you want.

Then, if you want to see more information about the file, you can use:

dd($request->file('thumbnail'));

Method 2

You can get the image with the $request->file('thumbnail')

 //Here is a sample to process the image
$image = $request->file('thumbnail');
$imageTitle = "Uploaded-file".".".$image->getClientOriginalExtension();
$imagePath = public_path('uploads');
$image->move($imagePath, $imageTitle);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x