首页 > 解决方案 > Laravel 数据库通知如何只显示那些在当月创建的通知(显示当月通知)

问题描述

laravel 数据库通知工作正常,但这里的问题是如何显示在系统的当年和月份创建的通知

<?php
$user = App\User::first();
foreach($user->unreadNotifications as $notification):?>
<h4>
    {{$notification->data['name']}}
    <small><i class="fa fa-clock-o">
        </i>{{$notification->created_at->diffForHumans()}}</small>
</h4>
</a>
<?php endforeach;?>

标签: laravel

解决方案


取当前年月并将其与通知创建日期(仅限年月)进行比较。这是代码

<?php
$user = App\User::first();
$currentMonth = date('Y-m');
foreach($user->unreadNotifications as $notification):
if(date('Y-m',strtotime($notification->created_at)) == $currentMonth);?>
<h4>
    {{$notification->data['name']}}
    <small><i class="fa fa-clock-o">
        </i>{{$notification->created_at->diffForHumans()}}</small>
</h4>
</a>
<?php endif;
endforeach;?>

推荐阅读