user1797484
user1797484

Reputation: 758

Formating JSON response for JqueryUI Autocomplete

I am trying to form that proper response to this but am having trouble. Standard is here: https://github.com/devbridge/jQuery-Autocomplete

    $query = Input::get('query');

    $query = $query . "%";
    $categories = Category::select('name', 'id')->where('name', 'like', $query)->get();

    //$suggestion = array();
    foreach($categories as $category){

        $suggestion['value'] = $item['value'] = $category->name;
        $suggestion['data'] = $item['data'] = $category->id;


    }
    $suggestions = array('suggestions' => $suggestion);





    return Response::json($suggestions);

Upvotes: 0

Views: 102

Answers (1)

Bram
Bram

Reputation: 4532

You need to supply an array of suggestions, now you just overwrite the suggestion the whole time and returning one of them. Something like this:

$suggestions = array();
foreach($categories as $category){

    $suggestion['value'] = $item['value'] = $category->name;
    $suggestion['data'] = $item['data'] = $category->id;

    $suggestions[] = $suggestion;
}
return array('suggestions' => $suggestions);

Upvotes: 1

Related Questions