我有以下型号:
class Recipe extends Model
{
public function parts() {
return $this->hasMany('App\RecipePart');
}
}
class Ingredient extends Model
{
protected $fillable = [
'name','image'
];
}
class RecipePart extends Model
{
public $timestamps = false;
public function ingredients()
{
return $this->hasMany('App\RecipePartIngredient');
}
}
在我的控制器中,我想要返回一个食谱,其中所有食谱部分属于他,并且所有成分都属于每个食谱部分。以下代码有效,但......:
return Recipe::with('parts.ingredients')
->where('id',$request->id)->first();
它工作,我的parts.ingredients
返回我的recipe_part_ingredients
表中的数据,这是好的,但我想要每个ingredient_id - 从我的ingredients
表返回成分信息。 (姓名,图片)
知道我怎么能这样做吗?我尝试将以下代码添加到RecipePartIngredient中,但没有运气:
public function ingredient() {
return $this->hasOne('App\Ingredient');
}
这个怎么样:
return Recipe::with('parts.ingredients')
->with('parts.ingredients.ingredient')
->find($request->id);