htmllaravelinput-field

input type file not found the file but get null laravel


i have problem with my code. i make form to upload file to my database but my input type file get null object even if i have select image that i want

here my blade

<form action="/addcoaches" method="post">
            {{ csrf_field() }}
          <div class="input-field" style="margin-left:0;">
            <div class="form-group row">
                        <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('File') }}</label>
                        <div class="col-md-6">
                            <input type="file" name="filename" required>
                        </div>
                    </div>
                    <div class="form-group row mb-0">
                        <div class="col-md-6 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                {{ __('Upload') }}
                            </button>
                        </div>
                    </div>
          </div>
</form>

here my controller

public function saveCoach(Request $request)
    {
        $request->validate([
            'filename' => 'required',
            'filename.*' => 'mimes:doc,docx,PDF,pdf,jpg,jpeg,png|max:2000'
        ]);

        dd($request->file('filename'));
     }

here what my dd after i click upload button enter image description here

how to get the file from selected file. Thank you


Solution

  • Your encoding is wrong. When you make a POST request, you have to encode the data the right way. (link)

    In your case you want to encode your form with multipart/form-data

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