【发布时间】:2019-03-01 17:47:20
【问题描述】:
我有 3 个类似这样的类:
Exhibitor
'- Bills
'- Tickets
Bills 和 Tickets 都使用 SoftDeletes 特征,并且在 Exhibitor 类上我有这种关系:
public function tickets()
{
return $this->hasManyThrough(Ticket::class, Bill::class);
}
我需要获取所有门票,包括已删除的门票 (withTrashed),但这也需要包括所有已删除的票据。问题是,withTrashed 仅适用于门票模型,而不适用于票据。
这个查询
$tickets = exhibitor()->tickets()->withTrashed()
->where('bills.updated_at', '>=', Carbon::parse($since))
->orderBy('tickets.id')
->get();
生成此 SQL
select `tickets`.*, `bills`.`exhibitor_id` from `tickets`
inner join `bills` on `bills`.`id` = `tickets`.`bill_id`
where `bills`.`deleted_at` is null
and `bills`.`exhibitor_id` = ?
and `bills`.`updated_at` >= ?
order by `tickets`.`id` asc
而我应该需要这个没有“bills.deleted_at is null”的SQL,如下所示:
select `tickets`.*, `bills`.`exhibitor_id` from `tickets`
inner join `bills` on `bills`.`id` = `tickets`.`bill_id`
where `bills`.`exhibitor_id` = ?
and `bills`.`updated_at` >= ?
order by `tickets`.`id` asc
但我没有看到任何可以为 Bill 模型设置 withTrashed() 的选项。我认为应该可以在 hasManyThrough 方法上设置回调查询,但根据 API,不支持此功能。这看起来很简单,我感觉我忽略了一些东西,但我找不到它......
【问题讨论】:
标签: laravel eloquent laravel-5.5