php - 在 Laravel 中使用 () 获取数据嵌套关系
问题描述
我有一个包含关系的模型文件“学生”:
public function implementations()
{
return $this->hasMany(Implementation::class);
}
在我的实施模型中,我有这些关系:
public function score()
{
return $this->belongsTo(Score::class, 'id', 'implementation_id');
}
public function project()
{
return $this->belongsTo(Project::class);
}
我想检索一个包含所有数据的表。
我试过这个
public function getStudents($id)
{
$event = Event::where('id', $id)->first();
$data = $event->students()->with('implementations')->get();
return response()->json($data);
}
有用。但我没有我想要的结果。我也想用and关系恢复implementations
数据project
score
我试过了,但它不起作用
public function getStudents($id)
{
$event = Event::where('id', $id)->first();
$data = $event->students()->with('implementations')->with('project', 'score')->get();
return response()->json($data);
}
非常感谢您的帮助!
解决方案
你试过Event::with('students', 'students.implementations')
了吗?您可以使用点符号预先加载关系的关系。
推荐阅读
- windows - 使用文本文件更改文件夹中图片的名称
- php - jQuery函数不调用模态关闭
- google-chrome-devtools - 打印不可变记录(启用不可变对象格式化程序扩展)不会打印任何内容
- android - 创建空的 txt 文件夹而不是 txt 文件
- reactjs - 如何将api接收数据保存在局部变量中
- python-3.x - 在打印两个系列数字之间交替
- postgresql - 选择查询的 RDS CPU 利用率高
- python - 如果 python 请求发生错误,如何获取状态码?
- javascript - 我如何检测太鼓 js 中的按钮
- function - React Native - 需要源图像中的Concat字符串,不起作用