Estoy usando ajax para la búsqueda en vivo, pero el problema es que solo se muestra un resultado
cuando estoy usando .html() pero cuando estoy usando append() funciona, pero cada palabra que escribo para duplicar los resultados,
aquí está mi código:
en controlador,
$patient = Patient::select('id', 'avatar') ->where('phone_number', 'like', '%' . $search_query . '%') ->orWhere('first_name', 'like', '%' . $search_query . '%') ->limit(15) ->get(); return $patient;codigo ajax en 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();; } });Sí, configura su contenido en su declaración de bucle, por lo que solo tomará el último contenido.
Puedes usar alguna variable de búfer:
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); // ....