mercredi 30 septembre 2015

Laravel Autocomplete retruning undefined offset: 0

I am trying to implement autocomplete in on of the forms using Laravel.

My route is setup as:

Route::get('employee/getReportAuth', 'EmployeeController@getAuthority');

View:

<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
     <input class="mdl-textfield__input" type="text" name="reportTo" id="reportTo" />
     <label class="mdl-textfield__label" for="reportTo">Reporting Authority</label>
</div>

Javascript:

$("#reportTo").autocomplete({
    source: "{{url()}}/employee/getReportAuth",
    minLength: 3,
    select: function(event, ui) {
        $('#reportTo').val(ui.item.value);
    }
});

Controller:

public function getAuthority() {
        $term = Input::get('term');
        $reportTo = array();
        $search = Employee::where('firstName', 'like', '%'.$term.'%')->get();
        foreach ($search as $result) {
            $reportTo[] = $result;
        }
        return Response::json($reportTo);
    }

However, irrespective of the route or controller. As soon as the autocomplete function is triggered, the network tab in the inspect element shows me that the page is throwing a 500 error. Stating undefined offset: 0

My reference of autocomplete code has been from here.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire