i am get json encoded data in id text pair results: [{id:2, text:3}, {id:21, text:654}, {id:23, text:12343}, {id:24, text:4536}, {id:28, text:987},…] total: 13
my select2 code
$("#model_no_search").select2({
ajax: {
url: 'get_model_no',
dataType: "json",
type: "POST",
quietMillis: 500,
data: function (term, page) {
return {
str:term
};
},
results: function (data, page) {
console.log(data.results);
return { results: data.results };
}
},
allowClear: true,
});
get_model_no function
public function get_model_no($str=0) {
$data['total'] = Bag_model::leftJoin('category', 'bag.cat_id', '=', 'category.id')
->select('model_no AS text','model_id AS id')->get()->count();
$data['results'] = Bag_model::leftJoin('category', 'bag.cat_id', '=', 'category.id')
->select('model_id AS id','model_no AS text')->get()->toArray();
echo json_encode($data);
}
in route
Route::post('get_model_no', 'Bag@get_model_no');
your help is appriciated. thanks in advance.