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

vandan's avatar
Level 13

how to count in laravel

How To Count My Total Notifications

0 likes
9 replies
jlrdw's avatar

Not knowing your exact set up, here is what a basic group by might look like with a count:

$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
                ->select('dc_powners.ownerid', 'dc_powners.oname')
                ->selectRaw('count(dc_pets.petid) as countOfPets')
                ->groupby('dc_powners.ownerid')
                ->orderby('dc_powners.oname')
                ->get();

Results basically give:

ownerid, oname, countOfPets

Like:

5|Bob|3
4|Greg|9
2|Rob|1

In example dc_powners is parent table.

Also notice the aggregate count uses selectRaw

1 like
vandan's avatar
Level 13

@jlrdw

Thank you sir for reply i use laravel notification so how can i count

here is my blade file

@foreach (Auth::user()->Notifications as $notification)                                 
        {{ $notification->data['message'] }}
    @endforeach 

so i dont understand how to count

mushood's avatar
@foreach (Auth::user()->Notifications as $key => $notification)                                 
        {{$key+1}}. {{ $notification->data['message'] }}
    @endforeach 

foreach gives you the key for the array. If indexed, you can use this as a counter when looping

1 like
vandan's avatar
Level 13

@mushood @jlrdw

i try this but not display any count notification and also my notification data also not display

vandan's avatar
Level 13

@mushood @jlrdw

try this but output like i have total 3 record of notification but output like 1 1 1

so i want output like total 3 but display out[ut is now 1 1 1

so how to do it total no of count notification

here is my code

blade file

@foreach($users as $not)  
        @foreach ($not->Notifications as $key => $notification)
                {{$key+1}}
            @endforeach
@endforeach 

my controller file like

$users = \Auth::user()->where('role','=','user')->get();
    return view('admin.pages.dashboard',compact('users'));
joaorbrandao's avatar
Level 5

Hi @van-india ! First of all you're using the Auth::user() to get the logged in user and taking advantage of being that result being a User Model to query the database to get all users with the given condition. It works, but try to think how wierd that can be for your future self :)

Now about your problem. I assumed that you have a "Notifications" method in your User's Model that reflects the database connection between those two entities. Also assumed that in the notifications table you have the "message" column, so it will be reflected in you model's instance. The $loop->iteration can be found in the documentation here: https://laravel.com/docs/6.x/blade#the-loop-variable Check the example.

// Before
@foreach($users as $not)  
        @foreach ($not->Notifications as $key => $notification)
                {{$key+1}}
            @endforeach
@endforeach 

// After
@foreach($users as $user) 
    $totalUserNotifications = count($user->Notifications) ;
        @foreach ($user->Notifications as $notification)
                {{ $loop->iteration }} / {{ $totalUserNotifications }} - {{ $notification->message}}
            @endforeach
@endforeach 
// Output:
// 1 / 3 - First Notification Message
// 2 / 3 - Second Notification Message
// 3 / 3 - Third Notification Message
1 like
vandan's avatar
Level 13

@joaorbrandao

ok i undertand but my question is

i want all the notification display in my blade file so how can i display without auth beacuse i have all the notification display and also count so how can i

Snapey's avatar

Were you just thinking of count() ?

<span>{{ Auth::user()->Notifications->count() }}</span>

@foreach (Auth::user()->Notifications as $notification)                                 
    {{ $notification->data['message'] }}
@endforeach
1 like
joaorbrandao's avatar

Without the Auth you can directly use the User model.

// Assuming your where condition...
$users = User::where('role', '=', 'user')->get();

// Now to get all user's notifications 
// Iterate over $users as $user
$user->notifications

Please or to participate in this conversation.