繁体   English   中英

从网桥表Laravel检索数据

[英]Retrieve data from bridge table laravel

D B

我在从“ ingredient_recipe”表中检索“ part ”数据时遇到问题。 有什么方法可以在不使用查询生成器的情况下获取“ part ”数据? 这是我在配方模型中检索成分的代码。 问题是我想从配方模型中检索份值。

class Recipe extends Model
{
  public function ingredients(){
        return $this->belongsToMany('App\Ingredient');
  }
}

在关系中添加数据透视表列

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

然后像访问它

$recipe = Recipe::find(1);

foreach ($recipe->ingredients as $ingredient) {
    echo $ingredient->pivot->portion;
}

暂无
暂无

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

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