带Select2的Laravel 5,用ajax加载数据


Laravel 5 with Select2, Loading data with ajax

首先,这是我到目前为止的代码。

我的路线:

Route::get('cities/citybylang/{lang_id}', [
        'uses'  => 'CitiesController@cityByLanguage',
        'as'    => 'dashboard.cityByLanguage'
]);

我的控制器:

public function cityByLanguage($lang_id){
    $cities = City::select('name','id')->where('lang_id',$lang_id)->get();
    return $cities;
}

视图选择

<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;
            }

好的,基本的问题是,我不能传递正确的参数给控制器。这就是现在的工作方式:我开始输入select字段,它给出了一个包含lang_id = 1的城市列表。
ajax调用把这个发送给控制器:
Somthing.com/dashboard/cities/citybylang/1?q= [value,我在select字段中输入的内容]
我有一个漂亮的url,所以我想要这样的东西:
Somthing.com/dashboard/cities/citybylang/[value,我在select字段中输入的内容]


问题是,我怎样才能以正确的方式将参数传递给控制器?

这个问题是旧的,但我正在寻找同样的东西,这就是我如何管理它。

url: function(params) {
    return '/some/url/' + params.term;
},

这里是参考链接https://select2.github.io/options.html