$categories= Category::withCount('orders')->get(); // Returns categories with number of orders count
create orders() function by following below relation
https://laravel.com/docs/8.x/eloquent-relationships#has-many-through
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have four tables Order, Category, Product, Order_Product
each product belongs to a category each order has many products
i save the order products in order_product table,
now in orderResource i want to have number of order->products in each category so OrderResource should have
data
[
{
category_name: 'something',
productsCount: 3,
},
{
category_name: 'somethingelse',
productsCount: 2
}
]
how to do so ?
Please or to participate in this conversation.