如何限制Laravel中的通知数量

I have tried the ->limit(4) methods, but that doesn't seem to work

@foreach(auth()->user()->unreadNotifications()->limit(4) as $notification)
    <li>
        <a href="profile.html">
           <div>
              {{ $notification->data['message'] }}<span class="pull-right text-muted small">{{ time_elapsed_string($notification->created_at) }}</span>
           </div>
        </a>
    </li>
@endforeach

You do not execute the query here:

@foreach(auth()->user()->unreadNotifications()->take(4)->get() as $notification)

Also, you shouldn't run queries in Blade files.

It's hard to say what unreadNotifications really returns but if it's collection you could use:

@foreach(auth()->user()->unreadNotifications()->take(4) as $notification)

and if it's relationship you could use:

@foreach(auth()->user()->unreadNotifications()->take(4)->get() as $notification)

foreach(\Auth::user()->unreadNotifications->take(1) as $data){echo $data->id;}