[英]Retrieve data from bridge table laravel
I'm having an issue to retrieve " portion " data from table "ingredient_recipe". 我在从“ ingredient_recipe”表中检索“ part ”数据时遇到问题。 Is there any way to get the " portion " data without using Query Builder? 有什么方法可以在不使用查询生成器的情况下获取“ part ”数据? This is my code for retrieve ingredient in recipe model. 这是我在配方模型中检索成分的代码。 The problem is I want to retrieve portion value from Recipe model. 问题是我想从配方模型中检索份值。
class Recipe extends Model
{
public function ingredients(){
return $this->belongsToMany('App\Ingredient');
}
}
Add the pivot table column in your relationship 在关系中添加数据透视表列
public function ingredients()
{
return $this->belongsToMany('App\Ingredient')->withPivot('portion');
}
Then access it like 然后像访问它
$recipe = Recipe::find(1);
foreach ($recipe->ingredients as $ingredient) {
echo $ingredient->pivot->portion;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.