英文:
why aren't the entries deleting even if the condition meets in the where clause?
问题
我在控制台的Kernel.php文件中的计划函数中得到了这段代码:
$schedule->call(function () {
User::where('deleted_at', '<=', Carbon::now())->delete();
})->everyMinute();
它应该删除所有已经过期的deleted_at
时间变量的用户。我在本地服务器上使用php artisan schedule:work
命令启动了cron作业,因为我还没有服务器。这段代码每分钟执行一次,但什么都不会发生,也没有错误。我尝试使用简单的now()
,但仍然没有效果,我尝试设置本地时间而不是默认的UTC时间,但还是没有效果。
英文:
I got this code on the console Kernel.php file within the schedule function:
$schedule->call(function () {
User::where('deleted_at', '<=', Carbon::now())->delete();
})->everyMinute();
It should delete all the users that have passed the deleted_at time variable, I launch the cron job in my local server with the command php artisan schedule:work
since I have no server yet. The code executes every minute but nothing happens and there is no error. I tried with the simple now() but still nothing, I tried maybe setting the local time instead of the default UTC but also nothing
答案1
得分: 4
已软删除的模型 不会在常规 Eloquent 查询中显示出来,您需要使用 withTrashed()
或 onlyTrashed()
方法。如果您想永久删除所有已软删除的用户,我建议类似这样使用 forceDelete() 方法:
$schedule->call(fn() => User::onlyTrashed()->forceDelete())->everyMinute();
英文:
Soft deleted models do not show up with a regular Eloquent query, you need to use the withTrashed()
or onlyTrashed()
methods. If you're looking to permanently delete all the soft-deleted users, I'd suggest something like this, using the forceDelete()
method:
$schedule->call(fn() => User::onlyTrashed()->forceDelete())->everyMinute();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论