从网桥 table laravel 检索数据
Retrieve data from bridge table laravel
我在从 table "ingredient_recipe" 检索“部分”数据时遇到问题。有什么方法可以在不使用查询生成器的情况下获取“部分”数据吗?这是我在食谱模型中检索成分的代码。问题是我想从 Recipe 模型中检索部分值。
class Recipe extends Model
{
public function ingredients(){
return $this->belongsToMany('App\Ingredient');
}
}
在您的关系中添加枢轴 table 列
public function ingredients()
{
return $this->belongsToMany('App\Ingredient')->withPivot('portion');
}
然后点赞
$recipe = Recipe::find(1);
foreach ($recipe->ingredients as $ingredient) {
echo $ingredient->pivot->portion;
}
我在从 table "ingredient_recipe" 检索“部分”数据时遇到问题。有什么方法可以在不使用查询生成器的情况下获取“部分”数据吗?这是我在食谱模型中检索成分的代码。问题是我想从 Recipe 模型中检索部分值。
class Recipe extends Model
{
public function ingredients(){
return $this->belongsToMany('App\Ingredient');
}
}
在您的关系中添加枢轴 table 列
public function ingredients()
{
return $this->belongsToMany('App\Ingredient')->withPivot('portion');
}
然后点赞
$recipe = Recipe::find(1);
foreach ($recipe->ingredients as $ingredient) {
echo $ingredient->pivot->portion;
}