Skip to content
Advertisement

In laravel, how do I generate an array of counts?

For example, I have several orders, and each order is related to a zone(there are 10 zones). I want the number of orders for each zone so 10 rows. Something like this:

JavaScript

I tried using count and groupby methods but it generates an integer.

JavaScript

Any suggestions?

Advertisement

Answer

Try using withCount – laravel docs

You will need to add a relationship in your zone model, add the following

JavaScript

The eloquent query – the “orders” is the name of the method in your model (above)

JavaScript

Then in your blade file

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement