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

Error in displaying Category, subcategory & subsub category in Laravel

我的数据库中有 3 个表:"category"、"subcategory" 和 "subchild category",我想显示带有显示类别名称的 属性,如果产品在子类别下,则应显示子类别名称。请告诉我如何解决这个问题。

这是我的 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>

定义路线

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

属性控制器

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>

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