繁体   English   中英

Laravel 5.x,其中有许多附加的枢轴列

[英]Laravel 5.x whereHas many-to-many with an extra pivot column

我在食谱和配料之间设置了多对多数据透视表。 对于特定的成分,我想要一个清单

$ingredientID = 99;

$recipies = Recipe::whereHas('ingredients', function ($q) use ($ingredientID) {
    $q->where('id', '=', $ingredientID);
})->get();

那行得通。 但是在我的情况下,数据透视表中还有一个额外的列用于“描述”。 例如,特定的“ cookie”配方和“蛋”成分连接可能具有以下描述:“确保该配方的鸡蛋真的新鲜”。

那么,如何通过此查询返回该额外的数据透视表列? 我尝试进入模型文件并添加“ withPivot”调用,如下所示:

public function ingredients() 
{
    return $this->belongsToMany('App\Ingredient')->withPivot("description");
}

但这是行不通的。 知道我怎么能威吓我的whereHas查询咳嗽这个额外的数据透视表列吗?

要访问description数据透视列,您可以执行以下操作:

$recipies->first()->ingredients->first()->pivot->description;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM