【发布时间】:2018-11-14 17:33:28
【问题描述】:
这是我的情况:
在我的数据库中,我有一个表users。表中的行也有一个字段password_changed_at。现在我想选择 password_changed_at 字段超过 30 天的所有用户并发送推送通知但我不知道如何使用 Carbon 执行此操作。我的代码现在看起来像这样:
public function passwordExpired() {
$dateTime = new DateTime();
$currentDateTime = $dateTime->format('Y-m-d H:i');
$users = User::where('password_changed_at', $currentDateTime)->get();
// $user = $request->user();
foreach ($users as $user) {
$password_changed_at = new Carbon(($user->password_changed_at) ? $user->password_changed_at : "");
if (Carbon::now()->diffInDays($password_changed_at) >= 30) {
foreach ($password_changed_at as $password)
{
// $user = $user->id;
$user->notify(new ReminderPassword($user));
$push = new PushNotification('apn');
$push->setMessage([
'aps' => [
'alert' => 'Reminder for your password "'.$user->email.'"',
'sound' => 'default',
'badge' => $user->unreadNotifications->count()
],
'extraPayLoad' => [
'custom' => 'My custom data',
]
]);
$push->setDevicesToken($user->deviceToken);
$push->send();
$feedback = $push->getFeedback();
}
【问题讨论】:
-
语法错误:如果删除
foreach ($password_changed_at as $password) {会怎样?这没有必要。 -
您还应该在用户模型中将
password_change_at设置为“日期”。见:laravel.com/docs/5.7/eloquent-mutators#date-mutators