简体   繁体   中英

How to limit the number of notifications in 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;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM