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

miguelbc7's avatar

I need help with jquery autocomplete from database

I have a form and i try to use jquery autocomplete for get the products data but doesn't work

Here the form:

{!!Form::label('Product')!!}
{!!Form::text('product_id',null,['class'=>'form-control', 'id'=>'product', 'style'=>'-webkit-border-radius: 8px;-moz-border-radius: 8px;border-radius: 8px;'])!!}

Here the jquery:

<script>
$(document).ready(function() {
    $("#product").autocomplete({
        minLength:2,
        autoFocus: true,
        source: {{ URL('autocomplete') }}

    });
});
</script>

Here the route:

Route::get('autocomplete', 'DishesController@autocomplete');

Here the controller method:

public function autocomplete()
{
    $term = $_POST['term'];

    $results = array();

    $queries = Products::where('name', 'LIKE', '%'.$term.'%')->take(10)->get();

    foreach ($queries as $query)
    {
        $results[] = [ 'id' => $query->id, 'value' => $query->name];
    }
    return Response::json($results);
}

I would appreciate it if you could help me with this question.

0 likes
2 replies
tomopongrac's avatar

Try with lowercase

source: {{ url('autocomplete') }}
miguelbc7's avatar

doesn't work, in development tools in chrome, tab network show me that:

Uncaught SyntaxError: Unexpected token : 

Please or to participate in this conversation.