简体   繁体   English

无法获得有关加入多重雄辩关系的数据

[英]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表中的idFileNameFilePath以及User表中的idname以及course表列 - idcourseDisplayNameaboutCourse But it is returning null from both files & user relations.How can i fix this?但它从filesuser关系中都返回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您还必须选择外键fileIduserId才能使用filesuser的关系,因此查询将类似于

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM