我在Laravel 4.2上遇到问题,但在我的Google朋友上找不到任何解决方案:我想在有条件的情况下获取所有软删除的项目,并且根本无法使用...
这是代码:
$res = $this->model->whereHas('dsps', function ($q) use ($dsp_id) {
$q->where('dsps.id', $dsp_id);
$q->withTrashed();
})->get();
我得到以下SQL请求:
select * from `bdc` where (select count(*) from `dsps` inner join `bdc_dsp` on `dsps`.`id` = `bdc_dsp`.`dsp_id` where `bdc_dsp`.`bdc_id` = `bdc`.`id` and `dsps`.`id` = 81 and `dsps`.`deleted_at` is null) >= 1;
问题显然是仍然保留了“ deleted_at为空”。应该使用withTrashed()方法将其消失。
我试图把withTrashed()像这样:
$q->where('dsps.id', $sst_dsp)->withTrashed();
它并没有改变任何东西。
如何通过一个优雅的解决方案从我的whereHas请求中获得“ deleted_at为null”条件?在此先感谢您的建议!
一种解决方案是创建包含已删除记录的关系的另一种形式。
public function dspsTrashed() {
return $this->hasWhatever()->withTrashed();
}
然后:
$this->model->whereHas('dspsTrashed', ...);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句