Input field auto complete search select name and store the id in input box laravel

739 views Asked by At

Using laravel and jquery for input field autocomplete. I need to select name and store the id of that user in input field.

 <input id="show-user" type="text" class="typeahead form-control" name="student_code" value="{{ old('student_code') }}" placeholder="Student Code" required>

Here is javascript code

var path = "{{ url('library/issue-books/autocomplete/') }}";
    $('input.typeahead').typeahead({
        source:  function (query, process) {
            return $.get(path, { query: query }, function (data) {
                return process(data);
            });
        }
    });

Controller code for ptah

$data = User::where("name","LIKE","%{$request->input('query')}%")->get();
    return response()->json($data);

Now my problem is when I select name, need to send student code in request. Student_code integer column.

1

There are 1 answers

1
Misagh Laghaei On

You can send the input value to your route:

return $.get(path + $('#show-user').val(), {}, function (data) {
    return process(data);
});

In this way you should have this route syntax:

/library/issue-books/autocomplete/{$query}

But you can pass just a query string also:

return $.get(path + '?query=' + $('#show-user').val(), {}, function (data) {
    return process(data);
});