Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Cronix's avatar

From the manual (highlighed the important part):

Counting Related Models

If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models.

https://laravel.com/docs/5.6/eloquent-relationships#counting-related-models

So use 2 queries. The one that I helped with yesterday that we got working for pagination (not using withCount), and a separate query to get the count of all products in the category.

1 like
Snapey's avatar

What I meant all those posts ago...

Reverse the query. instead of category with products, get products belonging to category

1 like
Jonjie's avatar

When I saw your code:

 @foreach($search_results as $search_result)
                                @foreach ($search_result->products as $result)
                                Extracting data...
                                @endforeach
                            @endforeach
{{ $search_results->links() }}

I'm expecting the you're displaying the products. Have you tried this code?

 @foreach($search_results as $search_result)
     // Extracting data ...
 @endforeach
{!! $search_results->render() !!}
1 like
siusiak1000's avatar
Product::with('subcategories')->where('nazwa', 'nameProduct')->orderBy('id', 'desc')->paginate($liczba_paginacji);

I need:

Product::with('subcategories')->where('name', 'nameSubcategories')->orderBy('id', 'desc')->paginate($liczba_paginacji);

Model product:

    public function subcategories()
    {
        return $this->belongsToMany('App\MagazineSubcategory', 'product_has_subcategories', 'product_id', 'subcategory_id');
    }

@Snapey I do as you say. In this situation, I can not search for subcategories but for product names. How can I get a search by subcategories?

@jonjie - It will not work because I have a collection. It must be a loop in the loop.

Snapey's avatar

please consider whereHas in the docs

Ive got to go to work now so someone else will have to help you with an example

1 like
rumm.an's avatar

@snapey is right. Check your query. You are paginating your MagazineSubcategory not your Product. What you can do is find the subcategory you are looking for and then fetch related products. Something like this:

$magazineSubcategory = MagazineSubcategory::where('name', $subcategory)->firstOrFail();
$products = $magazineSubcategory->products()->paginate(3);

and if you need the products count you may still call count() on $products to get the count of products. This works in case, you need only one subcategory and all its related products paginated.

But, if you are thinking about fetching all the subcategories along with the paginated products you can surely do that, but that sounds wierd. There's no use of paginating each of subcategories' related products, instead you can use limit. If this is the case then, I can help. Just ask away.

1 like
Jonjie's avatar

Try this:

$search_results = MagazineSubcategory::whereHas('products', function ($query) {
            return $query->where('name', 'wiertarki');
        })->orderBy('id', 'desc')->paginate($liczba_paginacji);
1 like
siusiak1000's avatar

I use:

        $search_results = Product::whereHas('subcategories', function($query) use($subkategoria) {
            $query->where('name', $subkategoria);
        })->paginate($liczba_paginacji);

Thats all works!

Snapey's avatar

as I advised waaay back on page #1 ... hurray

3 likes
Cronix's avatar

@siusiak1000 It's not proper or polite to mark your own answer as correct when someone else pointed you in the right direction. You didn't discover that on your own and if @Snapey didn't point you in the right direction you most likely wouldn't have solved this. Be polite and give him the credit for the solution.

1 like
siusiak1000's avatar

I could follow @Snapey instructions. I thought I could do as I want.

Thank you very much! You are cool! This forum is amazing. The fastest help on earth. :)

Previous

Please or to participate in this conversation.