【问题标题】:how to create relational select option ajax in laravel 5.2如何在 laravel 5.2 中创建关系选择选项 ajax
【发布时间】:2018-12-03 06:43:16
【问题描述】:

我们想用ajax创建一个关系选择选项 Receive data from mysql on laravel 5.2.

我们有三个选择选项:“类型”、“品牌”和“产品名称”。

选择类型时,应加载相关品牌,然后在选择品牌时,应在最后一个选择中加载相关的产品名称。

【问题讨论】:

  • 欢迎来到 Stackoverflow。如果可能,请包含您的代码和数据的最小可重现示例。否则人们将很难帮助你

标签: php laravel


【解决方案1】:

我会尝试用一个例子来解释。

假设您有三个名为(国家、州、城市)的表,并假设您使用的是 jquery。

国家:

id | name          | code | **
--------------------------------
 1 | united states | US   | etc

状态:

id | country_id  | name
--------------------------------
 1 | 1           | Texas

城市:

id |state_id | name
--------------------------------
 1 | 1       | Houston

所以您将拥有三个模型,最好命名为 Country、State、City

我们需要两条路线来加载州和城市

Route::post( '/get/states', 'WorldController@states' )->name( 'loadStates' );
Route::post( '/get/cities', 'WorldController@cities' )->name( 'loadCities' );

在我们的控制器中(这里是 WorldController)我们有两个方法:

function states( Request $request )
{
      $this->validate( $request, [ 'id' => 'required|exists:countries,id' ] );
      $states = State::where('country_id', $request->get('id') )->get();
      //you can handle output in different ways, I just use a custom filled array. you may pluck data and directly output your data.
      $output = [];
      foreach( $states as $state )
      {
         $output[$state->id] = $state->name;
      }
      return $output;
}
function cities( Request $request )
{
      $this->validate( $request, [ 'id' => 'required|exists:states,id' ] );
      $cities = City::where('state_id', $request->get('id') )->get();
      $output = [];
      foreach( $cities as $city )
      {
         $output[$city->id] = $city->name;
      }
      return $output;
}

我们将有三个这样的选择选项:

<select name="country" id="country">
   @foreach( Country::get() as $country )
      <option value="{{ $country->id }}">{{ $country->name }}</option>
   @endforach
</select>
<select name="state" id="state"></option>
<select name="city" id="city"></option>

因为我们假设您使用的是 jquery,所以我们会像这样更新我们的选择选项:

<script>
$(function(){
    $('#country').change(function(){
       $("#state option").remove();
       $("#city option").remove();
       var id = $(this).value();
       $.ajax({
          url : '{{ route( 'loadStates' ) }}',
          data: {
            "_token": "{{ csrf_token() }}",
            "id": id
            },
          type: 'post',
          dataType: 'json',
          success: function( result )
          {
               $.each( result, function(k, v) {
                    $('#state').append($('<option>', {value:k, text:v}));
               });
          },
          error: function()
         {
             //handle errors
             alert('error...');
         }
       });
    });
    $('#state').change(function(){
       $("#city option").remove();
       var id = $(this).value();
       $.ajax({
          url : '{{ route( 'loadCities' ) }}',
          data: {
            "_token": "{{ csrf_token() }}",
            "id": id
            },
          type: 'post',
          dataType: 'json',
          success: function( result )
          {
               $.each( result, function(k, v) {
                    $('#city').append($('<option>', {value:k, text:v}));
               });
          },
          error: function()
         {
             //handle errors
             alert('error...');
         }
       });
    });
});
    </script>

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2020-04-24
    • 2016-05-27
    • 2018-08-22
    • 2021-12-08
    • 2021-06-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多