Set file required when edit image In Laravel?

  laravel, laravel-blade, php, upload

I have form to edit/upload image in Laravel Project.

<form action="{{ route('picture.update',$animal->sid) }}" enctype="multipart/form-data" method="POST">
 <div class="form-group">
                    <label class="form_group_title">Image</label>
                    <img id="file-image" src="{{asset('/images')}}/{{$animal->image}}" alt="Preview"
                        class="hidden img-thumbnail">
                    <input id="file-upload" type="file" name="image_upload" accept="image/*" class="hidden"
                        onchange="readURL(this);">
                    <label for="file-upload" id="file-drag">
                        <span class="btn btn-light">Select</span>
                    </label>
                    <div id="start">
                        <i class="fa fa-download" aria-hidden="true"></i>
                        <span class="text-danger">{{ $errors->first('image_upload') }}</span>
                    </div>
                </div>
</form>
 <script>
        function readURL(input, id) {
            id = id || '#file-image';
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function(e) {
                    $(id).attr('src', e.target.result);
                };

                reader.readAsDataURL(input.files[0]);
                $('#file-image').removeClass('hidden');
                $('#start').hide();
            }
        }
    </script>

The required is descried below:

$request->validate([
        'name' => 'required',
        'name_en' => 'required',
        'image_upload' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:500000',
        'date'  => 'required',
]);

How to set the default upload image if I don’t upload by reading image from database?

Source: Laravel StackOverflow

Leave a Reply