Abdul
Abdul

Reputation: 1607

Laravel, autocomplete using database

I am trying to do an ajax request to autocomplete the customer name, so far here's what I have: create.index.blade

    <div class="form-group">
                <strong>Customer id:</strong>
              <input class="typeahead form-control" type="text" name="customer_id"  placeholder="Customer id">
            </div>
<script type="text/javascript">
    var path = "{{ route('autocomplete') }}";
    $('input.typehead').typeahead({
        source:  function (query, process) {
        return $.get(path, { query: query }, function (data) {
                return process(data);
            });
        }
    });
</script>

here is my Assignee controller where the search route is:

use App\Customer;
public function autocomplete(Request $request)
      {
          $data = Customer::select("name")
                  ->where("name","LIKE","%{$request->input('query')}%")
                  ->get();

          return response()->json($data);
      }

here is my routes.php

Route::get('autocomplete', 'AssigneeController@autocomplete')->name('autocomplete');

I have a customer who's name is "test" whenever I am typing "tes" I am not getting prompted to auto complete it with test

Upvotes: 1

Views: 677

Answers (1)

Abdul
Abdul

Reputation: 1607

I forgot to add the typeahead bootstrap files. added it using the following link:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>

Upvotes: 1

Related Questions