简体   繁体   English

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

[英]Selecting a sub-category after selecting category using laravel

I am creating a form in which I get subcategories after I select one category. 我正在创建一个表单,在其中选择一个类别后将获得子类别。 But problem is that I have only one table in which categories and subcategories are stored. 但是问题是我只有一个表存储类别和子类别。

All the values in the table have their auto-increment id and a parent_id . 表中的所有值都有其自动增量idparent_id For all Main-Categories , parent_id is 0 . 对于所有Main-Categoriesparent_id0 and Sub-categories have Main-Categories id as their parent_id . Sub-categories具有Main-Categories id作为其parent_id As shown in image 如图所示

在此处输入图片说明

Depending upon this condition I build a query and passed that query to my view through controller as 根据这种情况,我建立了一个查询,并通过控制器将该查询传递给我的视图

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

And in view: 并且鉴于:

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

My script.js: 我的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: 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);
});

But when I use $a->name or $a->id , I get error as trying to get non object property. 但是当我使用$a->name$a->id ,由于尝试获取非对象属性而出现错误。

Also, I am taking refrence of this video 另外,我也很喜欢这部影片

for this change the name with id 为此更改名称为id

you set the id in the where field instead of name 您在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);
 });

As you need to find all Sub Category for a Main Category your query should be 因为您需要找到一个主类别的所有子类别,所以您的查询应该是

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

Here are some corrections taking reference from this page for script. 这是一些更正内容,可从此页面上获取脚本参考。 In Controller: 在控制器中:

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

View: 视图:

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

Script: 脚本:

        $(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 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);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM