如何在 laravel 的类别导航菜单中返回子类别

我已经使用 AppServiceProvider 对所有页面可用,我想在导航菜单中加载它们自己类别中的所有子类别,现在它将加载所有类别,在最后一个类别中,它将列出表中的所有子类别,请帮助。

这是图像样本

header.blade.php

@foreach($shareData['categories'] as $category)

      <li class="dropdown m-menu-fw">

        <a href="#" data-toggle="dropdown" class="dropdown-toggle">{{ $category->name }}

                                <span><i class="fa fa-angle-down"></i></span></a>


                                @endforeach

                                <ul class="dropdown-menu" >

                                    <li>

                                        <div class="m-menu-content" style="text-align: center;">

                                            <ul class="col-sm-12" >

                                                <li class="dropdown-header">{{ $category->name }}</li>

                                                @foreach($shareData['subcategories'] as $subcategory)

                                                <li><a href="#">{{ $subcategory->name }}</a></li>

                                                @endforeach

                                            </ul>




                                        </div>

                                    </li>

                                </ul>

                            </li>


AppServicePrivider.php


 $categories = Category::where('status',1)->get(); 

        $subcategories = Subcategory::where('status',1)->get();

$shareData = array( 

'categories'=>$categories,

'subcategories'=>$subcategories

);


       view()->share('shareData',$shareData);


类别.php


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Category extends Model

{

    protected $table = 'categories';



    public function posts(){

        return $this->hasMany('App\Post');

    }


    public function subcategory(){

        return $this->hasMany('App\Subcategory');

    }

}


慕码人2483693
浏览 153回答 2
2回答

汪汪一只猫

让你的 header.blade.php 像这样:@foreach($shareData['categories'] as $category)&nbsp; <li class="dropdown m-menu-fw">&nbsp; &nbsp; <a href="#" data-toggle="dropdown" class="dropdown-toggle">{{ $category->name }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span><i class="fa fa-angle-down"></i></span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="dropdown-menu" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="m-menu-content" style="text-align: center;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="col-sm-12" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="dropdown-header">{{ $category->name }}</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($category['subcategory'] as $subcategory)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="{{ url('/subcategory') }}/{{ $subcategory->id }}">{{ $subcategory->name }}</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>@endforeach使您的 AppServiceProvider 类似于以下代码:$categories = Category::where('status',1)->get();&nbsp;$shareData = array(&nbsp;'categories'=>$categories);&nbsp; &nbsp; &nbsp; &nbsp;view()->share('shareData',$shareData);

暮色呼如

您应该with('subcategory')直接在类别查询上使用:view()->share('shareData',Category::with('subcategory')->where('status',1)->get());那么您的刀片视图可能是:@foreach($shareData['categories'] as $category)&nbsp; &nbsp; <li class="dropdown m-menu-fw">&nbsp; &nbsp; &nbsp; &nbsp; <a href="#" data-toggle="dropdown" class="dropdown-toggle">{{ $category->name }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span><i class="fa fa-angle-down"></i></span></a>&nbsp; &nbsp; &nbsp; &nbsp; <ul class="dropdown-menu">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="m-menu-content" style="text-align: center;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="col-sm-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="dropdown-header">{{ $category->name }}</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($category['subcategory'] as $subcategory)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="#">{{ $subcategory->name }}</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </li>@endforeach我会将关系方法从 更改subcategory()为subcategories(),因为它是一种HasMany关系。
打开App,查看更多内容
随时随地看视频慕课网APP