让调度程序在 laravel 中删除从现在起 15 天前的通知?

Make scheduler to delete notifications that are 15 days old from now in laravel?

所以我有一个通知 table,它有一个 created_at 字段,我可以用它来删除从现在起 15 天前的通知。

我的调度程序代码:

$schedule->call(function () {

$now = \Carbon\Carbon::now();


DB::table('notifications')
             ->where($now->diffInDays('created_at'), '>', 15)
             ->delete();

    })->daily();
}

但这给出了错误:

DateTime::__construct(): Failed to parse time string (created_at) at position 0 (c): The timezone could not be foun
  d in the database

我该如何解决?还有其他方法只使用 php 吗?

您遇到的错误是因为您试图区分 Carbon 对象和字符串 ('created_at')。您可以通过像这样

更改 where 子句来解决此问题
->where('created_at', '<', $now->subDays(15))

或者您可以改为编写原始查询。