Relationship: Products, Categories and Subcategories
<p>I'm new to laravel 5.2. </p>

<p>Would anyone help me out with an example  to define model relationship between categories - subcategories - products and how to fetch products under subcategories by relevant id? </p>

<pre><code>    // Category  model
public function subcategories(){ return $this-&gt;hasMany('App\Subcategory', 'category_id'); }    
public function products() {return $this-&gt;hasMany('App\Product', 'subcategory_id');}

// Subcategory model 
public function categories() {return $this-&gt;belongsTo('Category');}
public function products() {return $this-&gt;hasManyThrough('App\Product'
,'App\Category','subcategory_id','category_id','id');}

// Product model
public function categories() {   return $this-&gt;belongsTo('App\Category');}
public function subcategories() {return $this-&gt;belongsTo('App\Subcategory');}
</code></pre>

<hr>

<pre><code>// my controller
// class to fetch subcategories under category
public function show_subcategory($name){  
$categories=Category::whereName($name)-&gt;get(); 
return view('project.show_subcategory')-&gt;with('categories',$categories); 
} 

// class to fetch product under subcategories    
public function show_product($id){ 
$subcategories = Subcategory::whereId($id)-&gt;get();
return view('project.show_product')-&gt;with('subcategories',$subcategories);}
</code></pre>

<hr>

<pre><code> //  views - show_subcategory.blade.php
@foreach( $categories as $category)
@foreach( $category-&gt;subcategories as $subcategory )    
{{$subcategory-&gt;name}}
@endforeach
@endforeach 

//  views - show_product.blade.php
@foreach( $subcategories as $subcategory) 
@foreach( $subcategory-&gt;products as $product ) 
{{$product-&gt;title}}
@endforeach 
@endforeach
</code></pre>

<hr>

<pre><code>Route::get('/show_subcategory/{name}','Project\MainController@show_subcategory');
Route::get('/show_product/{id}','Project\MainController@show_product');
</code></pre>

<hr>

<pre><code>// migrations - create_subcategories_table.php
$table-&gt;engine = 'InnoDB';
$table-&gt;increments('id');
$table-&gt;string('name');
$table-&gt;string('image');
$table-&gt;integer('category_id')-&gt;unsigned();
$table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade'); 
$table-&gt;timestamps();

// migrations - create_products_table.php
$table-&gt;engine = 'InnoDB';            
$table-&gt;increments('id');
$table-&gt;integer('subcategory_id')-&gt;unsigned();
$table-&gt;foreign('subcategory_id')-&gt;references('id')-&gt;on('subcategories')-&gt;onDelete('cascade'); 
$table-&gt;string('title');
$table-&gt;text('description');
$table-&gt;integer('price');
$table-&gt;boolean('availability')-&gt;default(1);
$table-&gt;string('image');
$table-&gt;timestamps();
</code></pre>

<blockquote>
  <p>note : class show_subcategory work well and i can display subcategories under categories</p>
  
  <p>but class show_product  dont work and i can't display products under
  subcategories .. the view dont show any errors and dont display
  products</p>
  
  <p>i can't know where is my error in model relation or in controller class or in view</p>
</blockquote>