dongxie3701 2015-12-18 07:38
浏览 10
已采纳

使用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. For all Main-Categories, parent_id is 0. and Sub-categories have Main-Categories id as their parent_id. As shown in image

enter image description here

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:

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

But when I use $a->name or $a->id, I get error as trying to get non object property.

Also, I am taking refrence of this video

  • 写回答

3条回答 默认 最新

  • dsbiw2911188 2015-12-22 10:53
    关注

    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

    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);});
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度