Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel HasManyThrough带三向枢轴台_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php Laravel HasManyThrough带三向枢轴台

Php Laravel HasManyThrough带三向枢轴台,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有以下型号 配方 public function ingredientRecipeUnits() { return $this->hasMany(IngredientRecipeUnit::class); } 成分 public function ingredientRecipeUnits() { return $this->hasMany(IngredientRecipeUnit::class); } public function ingredientRec

我有以下型号

配方

public function ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
成分

public function ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
public function ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
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 ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
public function ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
public function ingredient()
{
    return $this->belongsTo(Ingredient::class);
}

public function recipe()
{
    return $this->belongsTo(Recipe::class);
}

public function unit()
{
    return $this->belongsTo(Unit::class);
}
以及一个连接这三者的数据透视表(在其自己的模型中):

IngredEntercipeUnit

public function ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
public function ingredientRecipeUnits()
{
    return $this->hasMany(IngredientRecipeUnit::class);
}
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'
    );
}
这将生成一个如下所示的不正确的查询

从“配方”中选择*
内部连接“成分配方单位”
关于“配料配方单位”。`id`=`recipes`.`id`
其中“配料配方单位”。“配料id”=?
而实际上,查询应该是这样的。(注意第3行的
id
->
recipe\u id
的细微变化)

从“配方”中选择*
内部联接`成分►配方►单位`
关于“配料配方单位”。“配方id”=“配方”。“id”
其中“配料配方单位”。“配料id”=?

除了向雄辩的repo发送pull请求以添加额外参数或使用原始SQL之外;有什么方法可以解决这个问题吗?

这最终是我思考关系的方式中的一个错误,通过简单地定义一个
属性来解决,该属性直接从各个相关模型到
IngredEnterpireUnit

例如:成分模型

public function recipes() {
    return $this->belongsToMany(Recipe::class, 'ingredient_recipe_units');
}
根据您的模型,您可能有可能添加多个相同成分或单位,在这种情况下,您应该使用instinct方法标记查询

像这样:

public function recipes() {
    return $this->belongsToMany(Recipe::class, 'ingredient_recipe_units')->distinct();
}
哪个correclty生成以下所需查询:

select distinct * from `recipes` 
inner join `ingredient_recipe_units` 
on `recipes`.`id` = `ingredient_recipe_units`.`recipe_id` 
where `ingredient_recipe_units`.`ingredient_id` = ?

这最终是我思考关系的方式中的一个错误,通过简单地定义一个
belongTomany
,将单个相关模型直接定义到
IngredEnterpeunit
表中,就解决了这个问题

例如:成分模型

public function recipes() {
    return $this->belongsToMany(Recipe::class, 'ingredient_recipe_units');
}
根据您的模型,您可能有可能添加多个相同成分或单位,在这种情况下,您应该使用instinct方法标记查询

像这样:

public function recipes() {
    return $this->belongsToMany(Recipe::class, 'ingredient_recipe_units')->distinct();
}
哪个correclty生成以下所需查询:

select distinct * from `recipes` 
inner join `ingredient_recipe_units` 
on `recipes`.`id` = `ingredient_recipe_units`.`recipe_id` 
where `ingredient_recipe_units`.`ingredient_id` = ?