嵌套在雄辩的地方不起作用

时间:2018-02-23 19:30:50

标签: laravel eloquent lumen

您好我的流明应用程序中有以下代码:

$schedules = TravelSchedule::with(["route", "tickets.passenger"])
->whereHas("buses", function($query) use ($bus) {
    $query->where("bus_id", $bus->id);
})
->whereHas("route")
->whereHas("tickets", function($query) {
    $query->whereHas("invoice", function($query) {
        $query->whereNotNull("verified_at")
            ->whereNull("cancelled_at");
    })
    ->whereHas("passenger", function($query) {
        $query->whereNull("departed_at")
            ->whereNotNull("arrived_at");
    });
})->get();

我注意到whereNullwhereNotNull函数在运行代码时不起作用,我将它们取出并仍然返回相同的结果。雄辩不支持这种嵌套查询吗?如何重写此查询以便检查这些关系中的空字段和非空字段?

这是生成的sql:

select * from `travel_schedules`
where exists (select * from `buses`
  inner join `travel_schedule_bus`
    on `buses`.`id` = `travel_schedule_bus`.`bus_id`
    where `travel_schedules`.`id` = `travel_schedule_bus`.`travel_schedule_id`
    and `bus_id` = 137
    and `buses`.`deleted_at` is null)
and exists (select * from `travel_routes`
            where `travel_schedules`.`travel_route_id` = `travel_routes`.`id`
            and `travel_routes`.`deleted_at` is null)
and exists (select * from `sales_invoice_tickets`
            where `travel_schedules`.`id` = `sales_invoice_tickets`.`schedule_id`
            and exists (select * from `sales_invoices`
                        where `sales_invoice_tickets`.`sales_invoice_id` = `sales_invoices`.`id`
                        and `verified_at` is not null
                        and `cancelled_at` is null)
            and `sales_invoice_tickets`.`deleted_at` is null)
and exists (select * from `sales_invoice_tickets`
            where `travel_schedules`.`id` = `sales_invoice_tickets`.`schedule_id`
            and exists (select * from `passengers`
                        where `sales_invoice_tickets`.`id` = `passengers`.`ticket_id`
                        and `departed_at` is null
                        and `arrived_at` is not null)
            and `sales_invoice_tickets`.`deleted_at` is null)
and `travel_schedules`.`deleted_at` is null

0 个答案:

没有答案