1

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

5
  • Okay. What's the question? Commented Dec 29, 2018 at 16:18
  • code seems correct. so what's problem? Commented Dec 29, 2018 at 16:19
  • I am not getting auto complete.. not sure how can I even debug it to check if the AJAX request is being sent. Commented Dec 29, 2018 at 16:21
  • 1
    Take a look at your Network tab in Dev Tools. Additionally, could you console.log(data) to see what kind of response you are getting? Commented Dec 29, 2018 at 16:22
  • so I am getting the following error in my Network tab: Can't find variable: $ Commented Dec 29, 2018 at 16:36

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.