繁体   English   中英

如何限制Laravel中的通知数量

[英]How to limit the number of notifications in Laravel

我尝试过->limit(4)方法,但这似乎不起作用

@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

您不在此处执行查询:

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

此外,您不应在Blade文件中运行查询

很难说unreadNotifications真的会返回什么,但如果是它的集合你可以使用:

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

如果它的关系你可以使用:

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

试试这个吧

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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