簡體   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