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.