Я провел последние несколько часов в поисках решения этой проблемы и не нашел ничего подходящего. Я пытаюсь загрузить все маршруты, которым назначен хотя бы один самолет, который в настоящее время находится в аэропорту вылета маршрута, например:
Код: Выделить всё
Route::has('availableAircraft');
Код: Выделить всё
// Route.php
/**
* This returns all assigned aircraft that are not allocated to jobs and are at the departure airport of the route
*/
public function availableAircraft()
{
return $this->belongsToMany(
Aircraft::class, 'aircraft_route_assignments', 'route_id', 'aircraft_id')
->whereNull('current_job_id')
->where('current_airport_id', 'ROUTE_ID_HERE');
}
Я могу выполнить этот запрос с использованием необработанного SQL, но не могу найти способ воспроизвести это в Eloquent:
Код: Выделить всё
select
count(*) as aggregate
from
`routes`
where (
select
count(*)
from
`aircraft`
inner join `aircraft_route_assignments` on `aircraft`.`id` = `aircraft_route_assignments`.`aircraft_id`
where
`routes`.`id` = `aircraft_route_assignments`.`route_id`
and `current_job_id` is null
and `current_airport_id` = `routes`.`departure_airport_id`
) > 0
and `routes`.`deleted_at` is null
Что я пробовал
Я пытался вручную указать поле, как в SQL-запросе, но фактический SQL, сгенерированный этим, использует '
Код: Выделить всё
routes.departure_airport_idКод: Выделить всё
// Route.php
/**
* This returns all assigned aircraft that are not allocated to jobs and are at the departure airport of the route
*/
public function availableAircraft()
{
return $this->belongsToMany(
Aircraft::class, 'aircraft_route_assignments', 'route_id', 'aircraft_id')
->whereNull('current_job_id')
->where('current_airport_id', '`routes`.`departure_airport_id`');
}
Подробнее здесь: https://stackoverflow.com/questions/679 ... lationship
Мобильная версия