tehseen's avatar

fetch & display category and its products

I have relationship many to many between products & categories.

In my Product Model

public function categories() {
    	return $this->belongsToMany(Category::class, 'category_product');
    }

In my Category Model

 public function products() {
		return $this->belongsToMany(Product::class, 'category_product');
	}

so what i try to get is display category with products just like below image. https://ibb.co/vZy9HDz

each how have 4 categories and then each category display product like in image 2 rows with 4 product each.

in my controller i fetch category with product like below

        $categories = Category::with('products')->get();

Thanks for help in advance

0 likes
10 replies
tykus's avatar

You can use the Collection chunk method to group your categories into chunks of 4:

$categories = Category::with('products')->get() // returns an Eloquent collection
	->chunk(4); // returns a collection of collections

In the view:

@foreach ($categories as $chunk)
	
	<div class="row">
	
	@foreach ($chunk as $category)
		
		<div class="col-3">
		
		@foreach ($category->products as $product)

			// display product

		@endforeach
		</div> <!-- /col-3 -->
	@endforeach
	</div> <!-- /row -->
@endforeach
tehseen's avatar

@tykus Thanks,

in the most inner loop where we have

@foreach ($category->products as $product)

we get

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\Product Object
                (

display correct but loop disturb i need to get only 8 items if available so if i use array_chunk it will give error

thank for your help

tykus's avatar

There is no native Eloquent way to fetch only 8 products for each category, but there is a package by Staudenmeir which allows you to limit the products returned per category. You could chunk the products in each category in a similar way to the categories collection

tehseen's avatar

on my blade i write

@foreach ($categories as $chunk)
					<div class="row mb-4">
						@foreach ($chunk as $category)

							@if($category->products->count() > 0)

								<div class="col-md-3">
			                        <div class="silder-item">
										@foreach($category->products as $product)

											{{$product->product_name }}

										@endforeach
									</div>
									
								</div>

							@endif

						@endforeach
					</div>
				@endforeach
						

this works fine how ever the issue is row and column breaks like below in html image i don't know.

https://ibb.co/nnQ6djF

first row have 4 columns then empty row ? why this ?

Do you have any idea ?

tykus's avatar
tykus
Best Answer
Level 104

Is it possible that you have categories that have no products? Debug it.

You can guard against getting these categories at all by modifying the query:

$categories = Category::with('products')
	->has('products') // only returns categories that have some products
	->get()
	->chunk(4); 
1 like
tehseen's avatar

I guess this will worked.

And who i use paginate with this above query ?

tehseen's avatar

Thanks for help it woks the only remaining part is how i get previous nest pagination link with our above query

tehseen's avatar

so did we get pagination with chunk ?

Please or to participate in this conversation.