У меня есть следующие моделиLaravel HasManyThrough с 3-ходовым поворотным столом
Рецепт
public function ingredientRecipeUnits()
{
return $this->hasMany(IngredientRecipeUnit::class);
}
Ингредиент
public function ingredientRecipeUnits()
{
return $this->hasMany(IngredientRecipeUnit::class);
}
Unit
public function ingredientRecipeUnits()
{
return $this->hasMany(IngredientRecipeUnit::class);
}
и сводная таблица (в своей собственной модели), соединяющий все три:
IngredientRecipeUnit
public function ingredient()
{
return $this->belongsTo(Ingredient::class);
}
public function recipe()
{
return $this->belongsTo(Recipe::class);
}
public function unit()
{
return $this->belongsTo(Unit::class);
}
Я хотел бы получать все рецепты через модель ингредиента.
Для этого я сделал следующее соотношение:
public function recipes() {
return $this->hasManyThrough(
Recipe::class,
IngredientRecipeUnit::class,
'ingredient_id',
'id'
);
}
Это порождает неправильный запрос глядя, как этот
select * from `recipes`
inner join `ingredient_recipe_units`
on `ingredient_recipe_units`.`id` = `recipes`.`id`
where `ingredient_recipe_units`.`ingredient_id` = ?
в то время как в действительности этот запрос должен выглядеть следующим образом. (Обратите внимание на тонкие изменения id
->recipe_id
в строке 3)
select * from 'recipes'
inner join `ingredient_recipe_units`
on `ingredient_recipe_units`.`recipe_id` = `recipes`.`id`
where `ingredient_recipe_units`.`ingredient_id` = ?
Помимо отправку втягивания запроса на красноречивую репо, чтобы добавить дополнительный параметр или с использованием сырой SQL; есть ли способ решить это?