[英]Unable to get data on joining Multiple Eloquent Relationships
i wanted to display id
, FileName
& FilePath
from Files
table and id
& name
from User
table along with the course
table columns - id
, courseDisplayName
& aboutCourse
.我想显示
Files
表中的id
、 FileName
和FilePath
以及User
表中的id
和name
以及course
表列 - id
、 courseDisplayName
和aboutCourse
。 But it is returning null
from both files
& user
relations.How can i fix this?但它从
files
和user
关系中都返回null
。我该如何解决这个问题?
$course=Course::with(['files:id,FileName,FilePath','user:id,name'])
->select('id','courseDisplayName','aboutCourse')
->where('userId',$request->tutorId)
->get();
Course Model课程模式
public function files()
{
return $this->belongsTo(Files::class, 'fileId', 'id');
}
public function user()
{
return $this->belongsTo(User::class, 'userId', 'id');
}
This gives an output as below:这给出了如下输出:
[
{
"id": 20,
"courseDisplayName": "asasasb",
"aboutCourse": null,
"files": null,
"user": null
},
{
"id": 14,
"courseDisplayName": "yuu",
"aboutCourse": "kljkl",
"files": null,
"user": null
}
]
You will have to select the foreign keys fileId
and userId
too in order to use the relationship of files
and user
so the query will be like您还必须选择外键
fileId
和userId
才能使用files
和user
的关系,因此查询将类似于
$course=Course::with(['files:id,FileName,FilePath','user:id,name'])
->select('id','courseDisplayName','aboutCourse', 'userId', 'fileId')
->where('userId',$request->tutorId)
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.