簡體   English   中英

不顯示來自 Laravel Eloquent 關系的數據

[英]Not Displaying data from Laravel Eloquent Relationship

我想從Course表中選擇列idcourseNameuserId以及來自Files表的id,FilePath和來自user表的id, name & aboutUser 但是下面的代碼沒有從course table 返回任何內容,除了它顯示來自其他 2 個其他關系的數據。 我怎樣才能解決這個問題?

Course::with(['user' => function ($q) {
                $q->with('files:id,FilePath')->select('id', 'name','aboutUser');
            }])
            ->select('id','userId','courseName')
            ->where('id', $row)
            ->get();

課程模式

public function files()
    {
        return $this->belongsTo(Files::class, 'fileId', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'userId', 'id');
    }

下面給出的是我為上述代碼得到的輸出:

[
  0 => array:5 [
    "id" => 1
    "name" => "admin"
    "aboutUser" => abcd
    "files" => array:2 [
      "id" => 93
      "FilePath" => "Account.png"
    ]
  ]
 
]

請讓我知道,它的工作

$courses=Course::select('id','userId','courseName')->with(['user'=>function($q){
    return  $q->with('files:id,FilePath')->select('id', 'name','aboutUser');
}])->where('id', $row)->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM