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

时间:2017-10-04 17:47:01

标签: php laravel oop eloquent

我想在两个表,类别和子类别之间建立连接,以在导航栏中显示它们。 Parent_id对应于类别表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模型

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

Category.php模型

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

PostsController.php控制器

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

    }
}

Navbar.blade.php。与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

在这一刻它显示床床垫床垫。我怎样才能在第一个地方显示类别和子类别后?抱歉我的英文。

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您正在获取每个子类别的类别名称。

您可以在控制器中执行相反的操作,而不是使用类别获取子类别,您可以获得具有子类别的类别。

而不是:Subcategory::with('category')->get();

执行:Category::with('subcategory')->get();

然后在你看来,做一个正常的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