[英]Delete row from db after certain condition has been meet in laravel
My table has data id post_id description and dispose_time
. 我的表有数据
id post_id description and dispose_time
。 I wanted to delete data from DB if current time and date is greater than the current time. 如果当前时间和日期大于当前时间,我想从数据库中删除数据。
$post = Post::delete()->where('dispose_time' > DATE_SUB(NOW())), INTERVAL 10 MINUTE));
something like this . 这样的事情。 it is giving me the error and the dispose_time in my DB is in timestamp format something like this
1555107000
. 它给了我错误,我的数据库中的dispose_time是时间戳格式,如
1555107000
。 Any Help appreciated. 任何帮助赞赏。
Based on the information you provided, you should use the whereRaw
method, and not where
, since you are using SQL as where clause. 根据您提供的信息,您应该使用
whereRaw
方法,而不是where
,因为您使用SQL作为where子句。 Also, delete()
should be the last method to be called. 此外,
delete()
应该是最后一个被调用的方法。 So your code would be the following: 所以你的代码如下:
Post::whereRaw("`dispose_time` > DATE_SUB(NOW(), INTERVAL 10 MINUTE)")->delete();
Since you are using Laravel, it comes with the Carbon package and you can also do the following 由于您使用的是Laravel,它附带Carbon包,您还可以执行以下操作
Post::whereDate('dispose_time', '>', Carbon\Carbon::now()->addMinutes(-10))->delete();
最后使用delete子句,如下所示:
Post::where("dispose_time", ">", now()->addMinutes(-10)->toDateTimeString())->delete();
在laravel中使用碳时间。
$post = Post::where('dispose_time', '>', Carbon::now())->delete();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.