How to get the number of orders for each category?

Evgenii Duniak

I have 2 tables.

category (id, name, description, keywords, visibility) 
orders (id, category_id, price_id, name, telephone, date, processed) 

Model Category

public static function tableName()
{
    return 'category';
}
public function getOrders()
{
    return $this->hasMany(Orders::className(), ['category_id' => 'id']);
}

function getPrices()
{
    return $this->hasMany(Price::className(), ['category_id' => 'id']);
}

and Model Orders

public static function tableName()
{
    return 'orders';
}
public function getCategory()
{
    return $this->hasOne(Category::className(), ['id' => 'category_id']);
}


public function getPrice()
{
    return $this->hasOne(Price::className(), ['id' => 'price_id']);
}

I want to display the number of orders for each category when displaying categories for statistics. How to do it?

Tree building code

<?php  foreach ($cat as $item) :?>
                                        <ul class="treeview">
                                            <?php if ($item['id'] !=  17): ?>
                                                <li><a href="#"><?=$item['name']?> - 3</a>
                                                    <ul>
                                                        <?php  foreach ($price as $itemok) :?>
                                                            <?php if ($item['id'] ==  $itemok['category_id']): ?>
                                                                <li><a href="#"><?=$itemok['name']?></a></li>
                                                            <?php endif; ?>
                                                        <?php endforeach; ?>
                                                    </ul>
                                                </li>
                                            <?php endif; ?>
                                        </ul>
                                    <?php endforeach; ?>
Reborn

There are multiple ways you can do that see the example below

$categories = Category::find()->all();

foreach($categories as $category){
    echo count($category->orders);
    echo $category->getOrders()->count();
}

or create a separate method inside the model category

public function getOrderCount(){
    return $this->getOrders()->count();
}

and then use it like

$category->orderCount;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get continuous number based on item number for each category in excel?

From Dev

SQL Server How to get the number of news under each category

From Dev

SQL Server How to get the number of news under each category

From Dev

SQL Query: How do i get list of address and number of orders to each address

From Dev

Get total number of existing subcategories of each WooCommerce parent category

From Dev

How do I select an exact number of articles for each category?

From Dev

How to select all categories and count number of each category articles in MySQL

From Dev

How to select all categories and count number of each category articles in MySQL

From Dev

SQL Find the number of orders in each line count

From Dev

How get values categories an count products for each category in one query?

From Dev

How to get RANDOM records from each category in MySQL?

From Dev

Get sku from each orders Woocommerce

From Dev

How to get number of rows in a grouped-by category in pandas

From Dev

How to get all not answered orders

From Dev

How to reverse number orders in Excel to formate a date?

From Dev

Pandas groupby category, rating, get top value from each category?

From Dev

How to get multiple category posts in category page?

From Dev

Get each price per month for a category?

From Dev

query to get equal number of rows per category + sub category

From Dev

How to get user orders by date in woocommerce?

From Dev

How to get the next order id of orders in Magento

From Dev

How to get list of suppliers with most orders first

From Dev

How to get the Channel Type or Category of each YouTube channel using YouTube API?

From Dev

How to get the Channel Type or Category of each YouTube channel using YouTube API?

From Dev

How can I get 5 records for each 'category' with one select in MySQL?

From Dev

How can I get Access SQL to return a dataset of the largest value in each category?

From Dev

How to get ratio of sums 10% top and bottom each category from table?

From Dev

Get order number that makes the total sum of orders be 1000

From Dev

I want to know how many orders are placed by each of the customer

Related Related

  1. 1

    How to get continuous number based on item number for each category in excel?

  2. 2

    SQL Server How to get the number of news under each category

  3. 3

    SQL Server How to get the number of news under each category

  4. 4

    SQL Query: How do i get list of address and number of orders to each address

  5. 5

    Get total number of existing subcategories of each WooCommerce parent category

  6. 6

    How do I select an exact number of articles for each category?

  7. 7

    How to select all categories and count number of each category articles in MySQL

  8. 8

    How to select all categories and count number of each category articles in MySQL

  9. 9

    SQL Find the number of orders in each line count

  10. 10

    How get values categories an count products for each category in one query?

  11. 11

    How to get RANDOM records from each category in MySQL?

  12. 12

    Get sku from each orders Woocommerce

  13. 13

    How to get number of rows in a grouped-by category in pandas

  14. 14

    How to get all not answered orders

  15. 15

    How to reverse number orders in Excel to formate a date?

  16. 16

    Pandas groupby category, rating, get top value from each category?

  17. 17

    How to get multiple category posts in category page?

  18. 18

    Get each price per month for a category?

  19. 19

    query to get equal number of rows per category + sub category

  20. 20

    How to get user orders by date in woocommerce?

  21. 21

    How to get the next order id of orders in Magento

  22. 22

    How to get list of suppliers with most orders first

  23. 23

    How to get the Channel Type or Category of each YouTube channel using YouTube API?

  24. 24

    How to get the Channel Type or Category of each YouTube channel using YouTube API?

  25. 25

    How can I get 5 records for each 'category' with one select in MySQL?

  26. 26

    How can I get Access SQL to return a dataset of the largest value in each category?

  27. 27

    How to get ratio of sums 10% top and bottom each category from table?

  28. 28

    Get order number that makes the total sum of orders be 1000

  29. 29

    I want to know how many orders are placed by each of the customer

HotTag

Archive