在laravel中显示category、subcategory和subsub category时出错

wxclj1h5  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(332)

我的数据库中有3个表:“category”、“subcategory”和“subchild category”,我想用显示类别名称显示属性,其中,如果产品在subcategory下,则应显示子类别名称。请告诉我如何解决这个问题。
这是我的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>
v440hwme

v440hwme1#

定义路由

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>

还没有测试,但也许它会帮你一点。

相关问题