duandeng2011 2018-07-05 07:59
浏览 35
已采纳

让调度程序在laravel中删除15天后的通知?

So I have a notifications table which has a created_at field which I can use for deleting the notifications which are 15 days old from now.

My scheduler code:

  1. $schedule->call(function () {
  2. $now = \Carbon\Carbon::now();
  3. DB::table('notifications')
  4. ->where($now->diffInDays('created_at'), '>', 15)
  5. ->delete();
  6. })->daily();
  7. }

But this gives error:

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

How should I solve this ? And is there any other way using only php ?

  • 写回答

1条回答 默认 最新

  • doumalu9257 2018-07-05 12:59
    关注

    The error you are getting it's because you are trying to get difference between Carbon object and a string ('created_at'). You can fix this by changing where clause like this

    ->where('created_at', '<', $now->subDays(15))
    

    Or you can write raw query instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部