$('.catalog').select2({
placeholder: "--- Selected ---",
ajax: {
//for laravel csrf
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: 'api_url',
type:'post',
dataType: 'json',
processResults: function (data) {
return {
results: data
};
},
cache: true
},
});
<select class="form-select catalog" name="catlog">
<option value="{{$data->id}}" selected="selected">{{$data.name}}</option>
</select>
<?php
isset($_REQUEST['q']) ? $name = $_REQUEST['q'] : $name = '';
$row = ORM_DB::where('name', 'like', '%' . $name . '%')->get();
foreach ($row as $key => $value) {
$row_set[$key]["id"] = $value["id"];
$row_set[$key]["text"] = $value["name"];
}
echo json_encode($row_set);
}