I am having trouble getting this to make a response and I am not sure why. The select2 control seems to respod correctly but the function seems to be the problem....I think.
Any assistance would be appreciated.
ROUTE
Route::get('/api/selectcompany', 'CompanyController@selectcompany');
CONTROLLER
public function selectcompany(Request $request)
{
$term = trim($request->q);
if (empty($term)) {
return \Response::json();
}
$companies = Company::where('CompanyName', '%LIKE%', '%'.$term.'%')->paginate(20);
$formatted = [];
foreach ($companies as $company) {
$formatted[] = ['id' => $company->ID, 'text' => $company->CompanyName];
}
return \Response::json($formatted);
}
SCRIPT
<script>
$(document).ready(function() {
$('#companyid').select2({
placeholder: "Search for a company...",
minimumInputLength: 3,
ajax : {
url : '/api/selectcompany',
dataType : 'json',
delay : 200,
data : function(params){
return {
q : params.term,
page : params.page
};
},
processResults : function(data, params){
params.page = params.page || 1;
return {
results : data.data,
pagination: {
more : (params.page * 10) < data.total
}
};
}
}
});
});
</script>