来自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.

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