Laravel For Each循环中有许多只返回一条记录

时间:2018-05-12 00:21:53

标签: php laravel-5

我目前有两个型号设置: 分类和主要类别: 我的类别模型有:

return $this->belongsTo('App\MainCategory');

我的主要类别模型有:

return $this->hasMany('App\Category', 'main_category_id');

在我的控制器中,我得到了主要的类别数据:

$mainCategory = MainCategory::all();

return view('layouts.categories', ['mainCategory' => $mainCategory]);

在我的刀片中,我尝试访问每个主要类别及其子类别,如下所示:

                <aside id="sidebar_cate" class="col-sm-3 hidden-xs">
                    <h3 class="sidebar-title">Category</h3>
                @foreach ($mainCategory as $mCat)
                    <ul id="cate_list" class="cate_list">
                        <li class="level0 parent">
                            <a href="#" title="Business Cards">
                                <span>{{ $mCat->category_name }}</span>
                                <i class="fa fa-minus"></i><i class="fa fa-plus"></i>
                            </a>
                                @foreach ($mCat->categories as $category)
                            <ul class="level0">
                                <li class="level1 nav-1-1 first item">
                                    <a href="#" title="Premium Business Cards">
                                        {{ $category->title }}
                                        <span class="count-item">(22)</span>
                                    </a>
                                </li>
                            </ul>
                            @endforeach
                        </li>
                    </ul>
                    @endforeach

每个@foreach ($mCat->categories as $category)

当数据库具有多个具有相关键的记录时,

仅返回一条记录。我期待它能够返回每条记录。

Expected results:
Main Category
   Category 1 (has key that matches main category)
   Category 2 (has key that matches main category)
   Category 1 (has key that matches main category)
2nd Main Category
   Category 1 (has key that matches main category2)
   Category 2 (has key that matches main category2)
   Category 1 (has key that matches main category2)
etc...

我做错了什么?

0 个答案:

没有答案