简体   繁体   English

使用 Laravel Eloquent ORM 删除超过 30 天的行

[英]Delete Rows Older than 30 Days Using Laravel Eloquent ORM

I'm trying to delete the records from my database table that are older than 30 days.我正在尝试从我的数据库表中删除超过 30 天的记录。 I haven't executed the code because I wanted to check if I'm doing it right.我没有执行代码,因为我想检查我是否做对了。

App\MyTable::whereDate( 'created_at', '<=', now()->subDays( 30 ) )->delete();

Is that a correct way of deleting rows older than 30 days from a table?这是从表中删除超过 30 天的行的正确方法吗? Also, what would happen if it found zero records older than 30 days, would it throw an exception error or just run gracefully?此外,如果它发现零条记录超过 30 天,会发生什么情况,它会抛出异常错误还是正常运行?

  1. You have a typo.你打错了。 Try尝试

MyTable::whereDate( 'created_at', '<=', now()->subDays(30))->delete();

  1. It would run gracefully它会优雅地运行

Try this -尝试这个 -

$from= Carbon::now()->subDays(30)->toDateString();

$current = Carbon::now()->toDateString();

ModelName::whereBetween('created_at', array($diff,$current))
           ->delete();

Hope this works perfectly!希望这能完美运行! Please also let me know it works or not!也请让我知道它是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM