使用laravel选择类别后选择子类别

时间:2015-12-18 07:38:15

标签: php jquery ajax laravel-5

我正在创建一个表单,在我选择一个类别后,我会获得子类别。但问题是我只有一个表,其中存储了类别和子类别。

表格中的所有值都有自动递增idparent_id。对于所有Main-Categoriesparent_id0Sub-categories Main-Categories idparent_id。如图所示

enter image description here

根据这种情况,我构建一个查询并通过控制器将该查询传递给我的视图

$k = DB::table('main_category')->where('parent_id','=','0')->lists('name','id');

并且在视野中:

<div class="form-group">
    {!! Form::label('category','Category:') !!}
    <select name="category" id="category" class="form-control input-sm">
        @foreach($k as $a)
        <option value="{{$a}}">{{$a}}</option>
            @endforeach
    </select>
</div>

<div class="form-group">
    {!! Form::label('subcategory','Subcategory:') !!}
    <select name="subcategory" id="subcategory" class="form-control input-sm">
            <option value=""></option>
    </select>
</div>

我的 script.js:

    $('#category').on(change,function(e){
  console.log(e);
   var cat_id = e.target.value;

    //ajax
    $get('/ajax-subcat?cat_id='+ cat_id,function(data){
        //success data
        //console.log(data);
        $('#subcategory').empty();
        $.each(data,function(index,subcatObj){
            $('#subcategory').append('<option value ="'+subcatObj.id+'">'+subcatObj.name+'</option>');
        });

    });
});

routes.php文件:

    Route::resource('event', 'EventsController');

Route::get('/ajax-subcat',function () {
    $cat_id = Input::get('cat_id');
//    return $cat_id;
    $subcategories = DB::table('main_category')->where('name', '=',$cat_id)->get();
    return Response::json($subcategories);
});

但是当我使用$a->name$a->id时,我会因为尝试获取非对象属性而收到错误。

另外,我正在尊重this video

3 个答案:

答案 0 :(得分:0)

用于更改名称为

的名称

您在where字段中设置了id而不是名称

   Route::resource('event', 'EventsController');

    Route::get('/ajax-subcat',function () {
     $cat_id = Input::get('cat_id');
       //          return $cat_id;
      $subcategories = DB::table('main_category')->where('id', '=',$cat_id)->get();
return Response::json($subcategories);
 });

答案 1 :(得分:0)

由于您需要查找主要类别的所有子类别,因此您的查询应为

Route::get('/ajax-subcat',function () {
    $cat_id = Input::get('cat_id');
//    return $cat_id;
    //All the catagory belong to parentid is cat_id (main catagoryId)
    $subcategories = DB::table('main_category')->where('parent_id', '=',$cat_id)->get();
    return Response::json($subcategories);
});

答案 2 :(得分:0)

以下是针对脚本引用this page的一些更正。 在控制器中:

 $s = Category::all()->where('parent_id','=','0');

查看:

<div class="form-group">
    {!! Form::label('category','Category:') !!}
    <select name="category" id="category" class="form-control input-sm">
        @foreach($s as $k)
            <option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
        @endforeach
        {{--<option value="Dance And Music">Dance And Music</option>--}}
    </select>
</div>

<div class="form-group">
    {!! Form::label('subcategory','Subcategory:') !!}
    <select name="subcategory" id="subcategory" class="form-control input-sm">
        <option value=""></option>
    </select>
</div>

脚本:

        $(document).ready(function () { 
            $('#category').on('change',function(e){
            console.log(e);
            var cat_id = e.target.value;
            //console.log(cat_id);
            //ajax
            $.get('/ajax-subcat?cat_id='+ cat_id,function(data){
                //success data
               //console.log(data);
                var subcat =  $('#subcategory').empty();
                $.each(data,function(create,subcatObj){
                    var option = $('<option/>', {id:create, value:subcatObj});
                    subcat.append('<option value ="'+subcatObj+'">'+subcatObj+'</option>');
                });
            });
        });
    });

routes.php文件

Route::get('/ajax-subcat',function () {
$cat_id = Input::get('cat_id');
$subcategories = DB::table('main_category')->where('parent_id','=',$cat_id)->lists('name');
return Response::json($subcategories);});