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

CookieMonster's avatar

Count total number of orders

I am trying to get the number of orders made by a customer. The relationship is one purchase can have many orders.

This is how my code looks like:

Controller:


    public function customerAllOrders()
    {
        $user = User::find(Auth::user()->id);
        // Return purchases that user have paid
        $statuses = [3001, 3002, 3003];
        $purchases = $user->purchases->whereIn('purchase_status', $statuses);

// This line throws error
        $orderCount = $purchases->orders->get()->count();

        dd($orderCount);
        return view('shop.customer-dashboard.value-records.index')->with('purchases', $purchases)->with('orderCount', $orderCount);
    }

When I want to result the output, it says Property [orders] does not exist on this collection instance.

I understand I am trying to retrieve a collection of object but how do I get the number of records of the object?

For example, Purchase 1 -> 1 order , Purchase 2-> 2 orders, Purchase 3-> 1 order

So the count should return 4 orders as a result.

I don't see any issue with my code. What am I doing wrong?

0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@nickywan123 the problem is that your purchases is a collection instance, and on a collection you don't have an orders function, you will either need to iterate over each Purchase and call the orders count on it, and then sum the result, or maybe make the query a bit better like this:

$purchases = Purchase::where('user_id', $user->id)->whereIn('purchase_status', $statuses)
    ->withCount('orders')->get();

now in the resulting collection you will have a new field called orders_count which will contain the total number of orders for each purchase, then

$purchases->sum('orders_count'); // should be 4

NOTE I don't like the magic numbers btw, 3001, 3002, 3003 you should better name them and put a constant in order for you or another developer in the team to know what is going on :)

CookieMonster's avatar

Your solution works but I have a lot of question as I am unfamiliar with withCount . I don't see any documentation in Laravel for this. How does this work?

What does sum(orders_count) do? There is no column orders_count.

Pls explain.

Nakov's avatar

@nickywan123 here you go:

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

and each item of the collection will have orders_count not the collection itself.

So this:

$purchases->orders_count

won't work

this:

@foreach($purchases as $purchase)
{{ $purchase->orders_count }}
@endforeach 

will work.

You can pass it like this:

$totalOrders = $purchases->sum('orders_count'); // should be 4

return view('shop.customer-dashboard.value-records.index')->with('purchases', $purchases)->with('totalOrders', $totalOrders);

in the view:

{{ $totalOrders }}

should be the total.

Make a difference between a Collection $purcases and a single item of a collection which is an instance of a purchase.

CookieMonster's avatar

I passed it like this in my view:

   {{$purchases->sum('orders_count')}}

And somehow it returns the correct result?

CookieMonster's avatar
$totalOrders = $purchases->sum('orders_count');

This won't work since it says undefined variable orders_count

Please or to participate in this conversation.