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

t0berius's avatar

collection (sum where)

I'm struggeling with the following callback on a collection $userProfile->givenFeedbacks:

$userProfile->givenFeedbacks->sum(function ($feedback) {
            return count($feedback->where($feedback->shipping, null));
        });

I'm trying to get the amount of entries in the collection where shipping is set to null using the callback of sum().

Currently I get:

count(): Parameter must be an array or an object that implements Countable
0 likes
4 replies
tykus's avatar

Why does it get a Builder; what are they relationships?

t0berius's avatar

@tykus

I edited my first post, think it's now easier to understand.

Osta9's avatar

Shouldn't it be return count($feedback->where('shipping', null)); ?

tykus's avatar

Still not entirely clear what you are working with. Assuming a UserProfile hasMany Feedback, you should be able to use a Builder like this:

$countOfFeedbacks = $userProfile->givenFeedbacks()->where($feedback->shipping, null)->count();

Or, using a Collection:

$countOfFeedbacks = $userProfile->givenFeedbacks->where('shipping', null)->count();

I would tend towards the first approach for efficiency if you don't already have the Collection for another reason.

1 like

Please or to participate in this conversation.