Adding File or Image Uploads to Post Creation in Laravel



You have to updated your form HTML tag to include

<form method="POST" action="{{ route('') }}" enctype="multipart/form-data">

add file input inside the form

<div class="row mb-3">
    <label for="file" class="col-md-4 col-form-label text-md-end">{{ __('File') }}</label>

    <div class="col-md-6">
        <input id="file" type="file" class="form-control-file @error('file') is-invalid @enderror" name="file"
            value="{{ old('file') }}" required autocomplete="file" autofocus>

            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>

Update your Controller

add validation rules 

            'title' => 'required',
            'body' => 'required',
            'file' => 'required|mimes:txt,pdf|max:2097152'

Note: You need to be sure your php settings, php.ini allow for files of the size you ar trying to upload or the form validation will fail or you might get this error Illuminate \ Http \ Exceptions \ PostTooLargeException


//Maximum size of POST data that PHP will accept.
post_max_size = 2048M  
Maximum allowed size for uploaded files.                                                                                                          
upload_max_filesize = 2048M  

Ideally you'd refactor this out of your controller but leaving here for example