使用ajax将自定义消息发送到bootstrap div

I would like to send errors to my error-message div id using ajax. How to add it correctly to $('#error-message') in ajax function, that it would be recognised in Laravel's if statement?

Blade:

 <div class="flash-message" id="error-message">
    @if ($errors->any())
    @endif
 </div>

not working ajax code:

error: function(response) {
       var error = response.responseJSON.message;
       $('#error-message').html().show('<div class="flash-message">@if ($errors->any())<ul class="alert alert-danger">@foreach($errors->all() as $error)<li>'+ error +'</li>@endforeach</ul>@endif</div>'); 

I want my error message like that one:

enter image description here

Do something like that.

In your controller's function

$response = ['success' => true];

// Define your $rules according with validation rules and $messages accordingly
$validator = validator(request()->all(), $rules, $messages = []);

if($validator->fails()){
 return response()->json(['success' => false, 'errors' => $validator->messages()->all()])
}
//  your rest code for this function, which you want to do after checking validation error.

In your javascript ajax code

success: function(response) {
   if(!response.success){
   var error = response.errors;
   var errorRow = ''; 
   if(response.errors.length){
    $.each(response.errors, function(key, value){
     errorRow += '<li>'+ value +'</li>';
    });

    $('#error-message').html().show('<div class="flash-message"><ul class="alert alert-danger">'+errorRow+'</ul></div>');

   }
}

Inside error options of ajax, you can put simple text saying that, some server side error occurred.

Hope this will help :). Let me know if you have any problem