Php 在Laravel中使用()获取数据嵌套关系
我有一个模型文件“Student”,其中包含以下关系:Php 在Laravel中使用()获取数据嵌套关系,php,laravel,eloquent,Php,Laravel,Eloquent,我有一个模型文件“Student”,其中包含以下关系: public function implementations() { return $this->hasMany(Implementation::class); } 在我的实现模型中,我有以下关系: public function score() { return $this->belongsTo(Score::class, 'id', 'implementation_id');
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);
}
它起作用了。但是我没有我想要的结果。我还想用项目
和评分
关系恢复实施
数据
我试过了,但没用
public function getStudents($id)
{
$event = Event::where('id', $id)->first();
$data = $event->students()->with('implementations')->with('project', 'score')->get();
return response()->json($data);
}
非常感谢你的帮助 您尝试过使用('students'、'students.implements')事件::吗?您可以使用点表示法加载关系的关系。您尝试过使用('students','students.implements')的
事件::了吗?您可以使用点表示法加载关系的关系。您尝试过使用('students'、'students.implements')的Event::了吗
?您可以使用点表示法加载关系的关系。这很有用<代码>$data=$event->students()->with('implementations','implementations.project','implementations.score')->get()代码>谢谢!没问题。作为未来观众的答案发布。您是否尝试过使用('students'、'students.implements')
?您可以使用点表示法加载关系的关系。这很有用<代码>$data=$event->students()->with('implementations','implementations.project','implementations.score')->get()代码>谢谢!没问题。作为未来观众的答案发布。我使用此关系,无法在关系$sql->with(['postable'=>函数($sql)use($is_detail){if($is_detail&&in_数组(config('general.post_type_index.search'),['postable.post_type_id')){$sql->with([“postable”];}}}]);我使用这个关系,并且我无法在关系$sql->with(['postable'=>函数($sql)use($is_detail){if($is_detail&&in_数组(config('general.post_type_index.search'),[“postable.post_type_id]”)中使用急切加载关系值){$sql->with([“postable”];}}]);