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

时间:2020-04-13 21:50:50

标签: php laravel laravel-5.8

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

here is image sample

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);

Category.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');
    }
}


2 个答案:

答案 0 :(得分:1)

您应该在类别查询中直接使用with('subcategory')

view()->share('shareData',Category::with('subcategory')->where('status',1)->get());

那么您的刀片视图可能是:

@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>

        <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($category['subcategory'] as $subcategory)
                            <li><a href="#">{{ $subcategory->name }}</a></li>
                        @endforeach
                    </ul>
                </div>
            </li>
        </ul>
    </li>
@endforeach

由于它是一种subcategory()关系,因此我会将关联方法从subcategories()更改为HasMany

答案 1 :(得分:1)

使您的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>


                            <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($category['subcategory'] as $subcategory)
                                            <li><a href="{{ url('/subcategory') }}/{{ $subcategory->id }}">{{ $subcategory->name }}</a></li>
                                            @endforeach
                                        </ul>



                                    </div>
                                </li>
                            </ul>
                        </li>

@endforeach

使您的AppServiceProvider类似于以下代码:

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

$shareData = array( 
'categories'=>$categories
);

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