【发布时间】:2019-10-08 00:08:58
【问题描述】:
这很好用:
$post->comments()->withTrashed()->get()->each->delete();
因为它确实删除了 6 行,所以 each 作为集合返回。
但是,这会引发异常:
$post->comments()->withTrashed()->get()->each->restore();
我注意到它还在数据库中恢复了 1 行(第一行)(集合有 6 行)。
上下文:当我恢复帖子时,我在观察者中使用此类代码来恢复评论。
以下将抛出完全相同的异常InvalidArgumentException with message 'Illegale operator and value combination.':
$post->comments()->withTrashed()->where('deleted_at', '>=', $post->deleted_at)->get()->each->restore();
where() 条件允许我仅恢复与帖子软删除一起软删除的 cmets(这样它就不会恢复在整个帖子删除之前被版主删除的 cmets)。
【问题讨论】:
-
看起来你可能会遇到这个error 是相应的
$value为空。 The condition 可能会抛出错误的地方。 -
我发现了这个错误。观察者在
restored()Post 模型事件上触发。所以$post->deleted_at属性在第一次迭代后是null(这解释了为什么它只更新了第一个数据库行然后抛出异常,从那时起它在null 上执行>=。我设法快速修复将restored()更改为@987654335 @但我对此并不满意。我想在帖子恢复后恢复 cmets,而不是之前。
标签: php laravel eloquent laravel-6 php-7.3