jane
jane

Reputation: 25

How to count the same where method in Laravel 5.4 notifications to get the 2 notification types?

I'm trying to count two notification types, where i get notified when a user donates and when a user cancels it. how should I do it?

I have this and this returns 0 count:

{{ count(auth()->user()->unreadNotifications->where('type','App\Notifications\NewDonation')->where('type','App\Notifications\CancelDonation')) }}

Upvotes: 0

Views: 307

Answers (1)

Phil Cross
Phil Cross

Reputation: 9302

Try this:

auth()->user()
    ->unreadNotifications
    ->whereIn('notifiable_type', [
        'App\Notifications\NewDonation', 
        'App\Notifications\CancelDonation',
    ])
    ->count()

I think you've defined the type column incorrectly. by default, it's notifiable_type, not type

Also, in your original code snipper, you do ->where()->where().

Each time you call ->where(), it'll return a new collection. So the first where() will return all notifications where the type is the NewDonation type.

When you call where again for CancelDonation, the only records your collection will contain will be NewDonation notifications. therefore, after the second `where, it wont have any records found.

Upvotes: 2

Related Questions