Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Fajar's avatar
Level 2

Autocomplete using textbox

hello, how to make autocomplete in laravel

as an example

when I select in the textbox, there is an input form that is filled in

Thank you for the help

0 likes
3 replies
Jarjis's avatar

@fajar You can easily be done this using Typehead Js First, you need to create a function: Example: BookController.php

    public function autocomplete(Request $request)
    {
          $query = $request->get('query');
          $result = Book::where('title', 'LIKE', '%'. $query. '%')->get();
          return response()->json($result );
    } 

After that

        <div classs="form-group">
            <input type="text" id="search" name="search" placeholder="Search" class="form-control" />
        </div>
   
    <script type="text/javascript">
        var route = "{{ url('autocomplete') }}";
        $('#search').typeahead({
            source: function (query, process) {
                return $.get(route, {
                    query: query
                }, function (data) {
                    return process(data);
                });
            }
        });
    </script>
Fajar's avatar
Level 2

what i mean like this

input 1

<div class="col-md-4">
                                <div class="form-group">
                                    <label for="">Kode Klasifikasi</label>
                                    <select name="kode_klasifikasi" id="kodeklas" class="form-control">
                                        <option value="">Silahkan Masukan Kode Klasifikasi</option>
                                        @foreach($class as $clas)
                                            <option value="{{$clas->id}}">{{$clas->kode}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

input 2

<div class="col-md-4">
                                <div class="form-group">
                                    <label for="">Uraian</label>
                                    <input type="text" name="uraian" id="desc" style="visibility: hidden" class="form-control">
                                </div>
                            </div>

if i click option in input 1, data will appear in the input to 2

frankielee's avatar

I think this thread is related to javascript, not Laravel.

if you are using jquery, check the link https://api.jquery.com/change/

Example: if option is changed, set the input value = option value

$( "#kodeklas" )
  .change(function () {
		$("#desc").val($( "#kodeklas" ).val())
})

Please or to participate in this conversation.