在 Laravel 中让调度程序删除 15 天前的通知?

Raj*_*Raj 1 php laravel php-carbon laravel-5

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

我的调度程序代码:

$schedule->call(function () {

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


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

    })->daily();
}
Run Code Online (Sandbox Code Playgroud)

但这给出了错误:

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

我应该如何解决这个问题?还有其他仅使用 php 的方法吗?

Zor*_*ran 5

您收到的错误是因为您试图获取 Carbon 对象和字符串('created_at')之间的差异。您可以通过更改 where 子句来解决此问题,如下所示

->where('created_at', '<', $now->subDays(15))
Run Code Online (Sandbox Code Playgroud)

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