Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
search autocomplete ajax in laravel

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();;
        }
    });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

   // ....
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!