doudao3170 2017-10-04 17:47
浏览 19
已采纳

来自2张Laravel的不同行之间的关系

I want to make the connection between two tables, categories and subcategories, to display them in the navbar. Parent_id corresponds to the category table id

Table Categories

ID  Title
1.  Beds
2.  Mattresses

Table Subcategories

ID Title           Parent_id
1. Superimposed      1
2. Single            1
3. Arches            2
4. Foam              2

Subcategory.php model

Class Subcategory extends Model
{
    public function category()
    {
        return $this->belongsTo('App\Category');
    }
}

Category.php Model

class Category extends Model
{
    public function subcategory()
    {
        return $this->hasMany('App\Subcategory');
    }
}

PostsController.php Controller

class PostsController extends Controller
{
    public function index() 
    {
        $subcategory = Subcategory::with('category')->get();
        return view('posts.index', compact('subcategory'));

    }
}

Navbar.blade.php . Is linked with posts.blade.php

 <div class="navbar-collapse collapse" id="navigation">
            <ul class="nav navbar-nav navbar-left">
                <li class="active"><a href="">Acasa</a></li>
                  **@foreach($category as $subcat)**
                        <li class="dropdown yamm-fw">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="200">**{{$subcat->category->category}}**<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li>
                                    <div class="yamm-content">
                                        <div class="row">
                                            <div class="col-sm-3">
                                                <h5>Categorii</h5>
                                                <ul>
                                                  <li>
                                                    <a href="category.html">{**{Here i need to display the subcategories}**}</a>
                                                  </li>  
                                                    @endforeach
                                                </ul>
                                            </div><!-- /.yamm-content -->
                                        </div>
                                    </div>
                                </li>
                            </ul>
                        </li>

                @endforeach

In this moment it displays Beds Beds Mattresses Mattresses.How can I do to display in first place the categories and after the subcategories? Sorry for my english.

  • 写回答

1条回答 默认 最新

  • du0204 2017-10-04 17:58
    关注

    This is happening because you are getting the category name for each subcategory.

    You can do the inverse thing in your controller, instead of getting the subcategories with categories, you get the categories with subcategories.

    Instead of : Subcategory::with('category')->get();

    Do : Category::with('subcategory')->get();

    Then in your view, do a normal foreach :

    PostsController.php

    <?php
    class PostsController extends Controller
    {
        public function index() 
        {
            $categories = Category::with('subcategory')->get();
            return view('posts.index', compact('categories'));
        }
    }
    

    Index.blade.php

    @foreach($categories as $category)
        <li class="dropdown yamm-fw">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="200">
                **{{$category->title (or whatever the name is)}}**<b class="caret"></b>
             </a>
             <ul class="dropdown-menu">
                 <li>
                     <div class="yamm-content">
                         <div class="row">
                             <div class="col-sm-3">
                                 <h5>Categorii</h5>
                                     <ul>
                                         @foreach($category->subcategory as $sub)
                                             <li>
                                                 <a href="category.html">
                                                     {{ $sub->title (or whatever the name is)}}
                                                 </a>
                                              </li>  
                                          @endforeach
                                     </ul>
                                <!-- Closing tags here -->
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧