Reputation: 3375
I've made ability for administrator to create multiple categories under sub-categories. Now I want to show each product assigned to category.
The problem is that each sub-sub category show same product. Most probably the problem lays in database and the way they are stored but I'm not sure where exactly
So far this is my controller
public function showCategoryInfo($subcatId) {
$dual = DualSubCategories::where('sub_cat_id', '=', $subcatId)->get();
$dual_products = DualSubCategories::with('products')->findOrFail( $subcatId );
$subcat1 = SubCategories::with('products')->findOrFail( $subcatId );
return View::make('site.single_subcategory', [
'dual' => $dual,
'dual_products' => $dual_products
]);
}
This is my view
@foreach($dual as $dual_info)
<h3>{{ $dual_info['dual_sub_cat_name'] }}</h3>
<div class="row">
<div class="table-responsive">
<table class="table table-bordered">
<tbody>
@foreach($dual_products->products as $i => $product)
<tr>
<td>{{ $product['title'] }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endforeach
This is what I have in my model
public function subcategory()
{
return $this->belongsTo('SubCategories', 'sub_cat_id');
}
public function products()
{
return $this->hasMany('Product', 'dual_sub_cat_id');
}
Tables looks like this
// dual_category
dual_sub_cat_id sub_cat_id dual_sub_cat_name
1 1 test 1
2 1 test 2
// sub_category
sub_cat_id sub_cat_name
1 name 1
// products
product_id sub_cat_id dual_sub_cat_id
1 1 1
2 1 2
From table products
I have 2 products in same sub-category and when I open sub category with sub_cat_id = 1 I want to see on page
test 1
product_id 1
test 2
product_id 2
But I see
test 1
product_id 1
test 2
product_id 1
UPDATE with Models DualSubCategories Model
public function subcategory()
{
return $this->belongsTo('SubCategories', 'sub_cat_id');
}
public function products()
{
return $this->hasMany('Product', 'dual_sub_cat_id');
}
SubCategories Model
public function category()
{
return $this->belongsTo('Category', 'category_id');
}
public function products()
{
return $this->hasMany('Product', 'sub_cat_id');
}
Products Model
public function categories()
{
return $this->hasMany('Categories', 'category_id');
}
Upvotes: 2
Views: 1566
Reputation: 1384
There are a couple of things going wrong:
You do a loop and a nested loop. When the upper loop loops for a second time, it executes the inner loop with the same data as the first loop. The $dual_products
does not change a second or third time. It is not dependent on the upper loop.
But that does not explain why you see one product. In your controller, you call $dual_products = DualSubCategories::with('products')->findOrFail( $subcatId );
. That fetches one (findOrFail) DualSubCategory with the ID of the SubCategory. This does not fail because you called it with sub_cat_id = 1
and, coincidentally, that ID exists in the database.
If your relations are set-up right, this should work:
Controller
<?php
$dual = DualSubCategories::where('sub_cat_id', $subcatId)->with('products')->get();
return View::make('site.single_subcategory', ['dual' => $dual]);
We get the DualSubcategories with the products eager-loaded and assign that to the view.
View
@foreach($dual as $dual_info)
<h3>{{ $dual_info->dual_sub_cat_name }}</h3>
@foreach($dual_info->products as $i => $product)
{{ $product->title }}
@endforeach
@endforeach
In the view we loop over the DualSubcategories and display the name. Every loop, we loop over the products in that DualSubcategory and display it's title. (I've removed your formatting for clarity)
Upvotes: 2