【问题标题】:Not Displaying data from Laravel Eloquent Relationship不显示来自 Laravel Eloquent 关系的数据
【发布时间】:2021-12-13 10:21:01
【问题描述】:

我想从Course 表中选择列idcourseNameuserId,从Files 表中选择id,FilePath,从user 表中选择id, name & aboutUser。但是下面的代码没有从 course 表返回任何内容,除了它显示来自其他 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"
    ]
  ]
 
]

【问题讨论】:

    标签: php laravel eloquent laravel-8 eloquent-relationship


    【解决方案1】:

    请告诉我,这是工作吗

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

    【讨论】:

    • 不工作。我得到了和我以前一样的结果
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多