laravel - 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;?>
解决方案
取当前年月并将其与通知创建日期(仅限年月)进行比较。这是代码
<?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;?>
推荐阅读
- python-3.x - 学习使用 python 在亚马逊上抓取产品详细信息
- mysql - 提高文本列的搜索速度
- javascript - 有没有办法确定什么 javascript 对 DOM 元素进行了非常具体的更改
- php - 在 PHP 中访问 JSON 数组数据
- sql-server - 在返回查询中不尊重 upsert 子句的 Sequelize where 子句
- c++11 - 为转发类型创建包含 unique_ptr 的类的 shared_ptr 失败
- arrays - Swift 庞大的数组字典,非常慢
- ocaml - 在 OCaml 中同时使用 List.exists、List.for_all 和 List.filter
- python - 如何在 python 中使用 elasticsearch 来搜索所有索引?
- ms-access - VBA代码重命名文本文件,执行宏,然后重复(文件夹中的所有文件)