簡體   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