products with categories using category_products

Viewed 21

I have a Relation of products with categories using category_products as a pivot table. I want to get/take 5 products from each category but it not working correctly.

I have tried all the methods known to me I could find the solution

        $query = $this->getQuery();
        $query->where('parent_id','0')->with('categoryProducts',function($q1){
            $q1->with('product',function ($q){
                $q->orderBy('sold', 'desc');
            })->take(4);
        })->whereHas('categoryProducts');
        return $query->get();
0 Answers
Related