Reputation: 171
first of all, here is my code so far.
My route:
Route::get('cities/citybylang/{lang_id}', [
'uses' => 'CitiesController@cityByLanguage',
'as' => 'dashboard.cityByLanguage'
]);
My controller:
public function cityByLanguage($lang_id){
$cities = City::select('name','id')->where('lang_id',$lang_id)->get();
return $cities;
}
My view select
<select class="js-data-example-ajax">
<option value="3620194" selected="selected">select2/select2</option>
</select>
My Select2 code
$(".js-data-example-ajax").select2({
ajax: {
url: "/dashboard/cities/citybylang/1",
dataType: 'json',
delay: 250,
data: function (params) {
console.log(params.term);
return {
q: params.term, // search term
page: params.page,
name: params.name
};
},
beforeSend: function(jqXHR, settings) {
console.log(settings.url);
},
processResults: function (data, page) {
console.log(data);
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo, // omitted for brevity, see the source of this page
templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
function formatRepo (repo) {
if (repo.loading) return repo.text;
var markup = '<div class="clearfix">' +
'<div clas="col-sm-10">' +
'<div class="clearfix">' +
'<div class="col-sm-6">' + repo.name + '</div>' +
'<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.name + '</div>' +
'<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.name + '</div>' +
'</div>';
if (repo.name) {
markup += '<div>' + repo.name + '</div>';
}
markup += '</div></div>';
return markup;
}
function formatRepoSelection (repo) {
return repo.name || repo.text;
}
Okey, the basic problem is, i can't pass the right parameter to the controller.
This is how this work now: I start typing to the select field and it give a list with the cities which has the lang_id = 1.
So the ajax call send this to the controller:
somthing.com/dashboard/cities/citybylang/1?q=[value, what i type to the select field]
I have pretty url, so i want something like this:
somthing.com/dashboard/cities/citybylang/[value, what i type to the select field]
So the question is, how can i pass parameter to the controller in the right way?
Upvotes: 3
Views: 4925
Reputation: 760
This question is old but I was looking for same thing that's how I manged it.
url: function(params) {
return '/some/url/' + params.term;
},
Here is reference link https://select2.github.io/options.html
Upvotes: 1