laravel - Laravel 子关系
问题描述
我有以下型号:
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
表中的数据,这没关系,但我想要每个成分 ID - 从我的ingredients
表中返回成分信息。(名称,图像)。
知道我该怎么做吗?我尝试将以下代码添加到我的 RecipePartIngredient,但没有运气:
public function ingredient() {
return $this->hasOne('App\Ingredient');
}
解决方案
推荐阅读
- php - 查找字符串中第一个重复的单词
- .net-core - 为源重试“FindPackagesByIdAsync”
- intellij-idea - 错误:无法启动 javac.exe。exec:“javac.exe”:在 intellij 上运行仪表命令后,在 %PATH 中找不到可执行文件
- asp.net-core - 使用 AWS Cloudwatch 定期调用特定的 .net 核心 mvc 操作
- java - Android Recycler 分页:Room + RxJava + MVP
- python - 基于置信区间值的误差带等值线图
- icalendar - 使用 ical 文件取消事件
- python - 我如何解决''NoneType'对象不可下标'
- java - Maven 依赖:go-offline 命令问题
- ssl - 无法在 weblogic 服务器中进行 SSL 连接