I am using ajax for live searching, but the problem is that It is shown only one result
when I am using .html() but when I am using append() it works but every word i write it to duplicate the results,
here is my code:
in controller,
$patient = Patient::select('id', 'avatar')
->where('phone_number', 'like', '%' . $search_query . '%')
->orWhere('first_name', 'like', '%' . $search_query . '%')
->limit(15)
->get();
return $patient;
ajax code in blade
$("#search-eng").keyup(function() {
let search_query = $(this).val();
if (search_query != "") {
$.ajax({
url: '{{ url('/appointment/calander_patient_search') }}/'
search_query,
type: "GET",
dataType: "json",
success: function(data) {
$("#search-eng-show-list").show();
if (data !== "") {
$.each(data, function(key, value) {
$('#search-eng-show-list').html(
'<a data-id="' value.id '"' value.second_name '</a>');
});
}
if (data == "") {
$('#search-eng-show-list').html(
'<a><i "></i>No Record</a>'
);
}
},
});
} else {
$("#search-eng-show-list").empty();
$("#search-eng-show-list").hide();;
}
});
CodePudding user response:
Yes you set your content in your loop statement, so it will only take the last content.
You can use some buffer variable :
if (data !== "") {
var html = '';
$.each(data, function(key, value) {
html = '<a data-id="' value.id '"' value.second_name '</a>');
}
$('#search-eng-show-list').html(html);
// ....