Home > Mobile >  simple input type file not getting the file
simple input type file not getting the file

Time:02-02

Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller. im using laravel 9 with boostrap in it.

heres my code :

view

<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
    @csrf
    @method('POST')
    <div >
        <input id="file" name="file" type="file">
        <button type="submit" >{{ __('Save') }}</button>
    </div>
</form>

controller

public function put(Request $request){
    return $request;
}

i try to see the $request variable but the result is like this enter image description here

i try the solution like add enctype="multipart/form-data" but still didnt work.

thank you in advance

CodePudding user response:

I think you simply should get the request by its name or using `$request->all()`, you can do these two things:

In your controller you should write your put method like below:

 public function put(Request $request){
     $file = '';
     if($request->file('file'))
     {
       $file = $request->get('file);
     }
    
     return $file;
 }

or you can use the below code in your put method

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

Note: If you don't send the file as a JSON Response you should return it to view after saving operation; and also, there is no need to put @method('POST') inside the form;

  • Related