laravel 5.4

时间:2018-04-23 10:37:19

标签: php ajax laravel

我正在使用laravel 5.4的动态依赖下拉列表它在本地机器上运行良好但是当我把它放在托管上并被许多用户访问时,需要花费太长时间才能获得第二个下拉列表。

Fopm.php:

<div class="form-group col-md-4 col-sm-4 col-xs-6">
 {!! Form::label('id_country',trans('Country').":",['class' => 'labels']) !!}
 {!! Form::select('id_country',['' => '...'] +$countries->toArray(),Input::get('id_country'),['class' => 'js-example-basic-single form-control select2_country']) !!}
</div>
<div class="form-group  col-md-4 col-sm-4 col-xs-6">
 {!! Form::label('id_city',trans('City').":",['class' => 'labels']) !!}
 {!! Form::select('id_city',['' => '...']+$cities->toArray(),Input::get('id_city'),['class' => 'js-example-basic-single form-control select2_city']) !!}
</div>

的script.php:

sw = $('.select2_country').select2({
        placeholder: "Sélectionner",
        allowClear: true
    });
 sw.on("change", function (e) {
        id = $(this).select2('data')[0].id;
        $('.select2_city').val(null).trigger('change');
        $('.select2_city').select2({
            placeholder: "selectionner",
            allowClear: true,
            ajax: {
                url: '/api/caneva/cities?id=' + id
            }
        });

  });
  $('.select2_city').select2({
        placeholder: "selectionner",
        allowClear: true,
  });

Controller.php这样:

public function cities(SearchRequest $request)
{
    $id = $request->input('id');
    $city= DB::table('city')->select('libelle_city as text', 'id as id')
        ->where('id_country', '=', $id)
        ->get();
    return response()->json([
        'results' => $city
    ]);
}

0 个答案:

没有答案