在Laravel中显示类别,子类别和子子类别时出错

时间:2018-12-22 06:55:37

标签: php mysql laravel laravel-5.6 laravel-5.7

我的数据库中有3个表:“类别”,“子类别”和“子子类别”,我想显示带有显示类别名称的属性,如果产品在子类别下,则应显示子类别名称。请让我知道如何解决此问题。

这是我的propertcontroller.php:

$cat = Category::where('slug',$cat)->get(['id','category_name']);
$subcatid = Subcategory::where('slug',$subcat)->get(['id']);
$subsubcatid = Subsubcategory::where('slug',$subsubcat)->get(['id']);

这是我的查看代码:

<span class="tag pull-right">{{$cat}}</span>

1 个答案:

答案 0 :(得分:0)

定义路线

Route::get('dropdownlist','Propertcontroller@index');
Route::get('get-subcat-list','Propertcontroller@getSubcatList');
Route::get('get-subsubcat-list','Propertcontroller@getSubsubcatList');

Propertcontroller

public function index()
{
    $categories= DB::table("categories")->pluck("name","categories_id");
    return view('index',compact('categories'));
}

public function getSubcatList(Request $request)
{
    $subcategories= DB::table("subcategories")
    ->where("categories_id",$request->categories_id)
    ->pluck("name","subcategories_id");
    return response()->json($subcategories);
}

public function getSubsubcatList(Request $request)
{
    $subsubcat= DB::table("subsubcat")
    ->where("subcategories",$request->subcategories)
    ->pluck("name","subcategories_id");
    return response()->json($subsubcat);
}

您的索引

<select id="categories" name="categories_id" >
    <option value="" selected disabled>Select</option>
    @foreach($categories as $key => $category)
    <option value="{{$key}}"> {{$category}}</option>
    @endforeach
</select>

<select name="subcategories" id="subcategories"></select>
<select name="subsubcat" id="subsubcat"></select>

<script type="text/javascript">
    $('#categories').change(function(){
    var categoryID = $(this).val();    
    if(categoryID){
        $.ajax({
           type:"GET",
           url:"{{url('get-subcategories-list')}}?categories_id="+categoryID,
           success:function(res){               
            if(res){
                $("#subcategories").empty();
                $("#subcategories").append('<option>Select</option>');
                $.each(res,function(key,value){
                    $("#subcategories").append('<option value="'+key+'">'+value+'</option>');
                });

            }else{
               $("#subcategories").empty();
            }
           }
        });
    }else{
        $("#subcategories").empty();
        $("#subsubcat").empty();
    }      
   });
    $('#subcategories').on('change',function(){
    var subcatID = $(this).val();    
    if(subcatID){
        $.ajax({
           type:"GET",
           url:"{{url('get-subsubcat-list')}}?subcat_id="+subcatID,
           success:function(res){               
            if(res){
                $("#subsubcat").empty();
                $.each(res,function(key,value){
                    $("#subsubcat").append('<option value="'+key+'">'+value+'</option>');
                });

            }else{
               $("#subsubcat").empty();
            }
           }
        });
    }else{
        $("#subsubcat").empty();
    }

   });
</script>

尚未测试,但也许会对您有所帮助。