为什么即使在WHERE子句中满足条件,条目也不被删除?

huangapple go评论64阅读模式
英文:

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-&gt;call(function () {
        User::where(&#39;deleted_at&#39;, &#39;&lt;=&#39;, Carbon::now())-&gt;delete();
    })-&gt;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-&gt;call(fn() =&gt; User::onlyTrashed()-&gt;forceDelete())-&gt;everyMinute(); 

huangapple
  • 本文由 发表于 2023年6月8日 22:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定