如何在 Laravel 5.2 中创建关系选择选项 ajax

Arm*_*eri 1 php laravel

我们想在laravel 5.2上使用ajax从mysql中接收数据创建一个关系选择选项。

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

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

Rez*_*aSh 5

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

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

国家:

id | name          | code | **
--------------------------------
 1 | united states | US   | etc
Run Code Online (Sandbox Code Playgroud)

状态 :

id | country_id  | name
--------------------------------
 1 | 1           | Texas
Run Code Online (Sandbox Code Playgroud)

城市:

id |state_id | name
--------------------------------
 1 | 1       | Houston
Run Code Online (Sandbox Code Playgroud)

因此您将拥有三个模型,最好分别命名为 Country、State、City

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

Route::post( '/get/states', 'WorldController@states' )->name( 'loadStates' );
Route::post( '/get/cities', 'WorldController@cities' )->name( 'loadCities' );
Run Code Online (Sandbox Code Playgroud)

在我们的控制器(这里是 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;
}
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

因为我们假设您使用的是 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>
Run Code Online (Sandbox Code Playgroud)