【问题标题】:select query on with relations is not giving the results in laravel eloquent在关系上选择查询未在 laravel eloquent 中给出结果
【发布时间】:2018-11-04 19:12:08
【问题描述】:
records = Student::with(array('Mark' => function($marks)
    {
    $marks->select(DB::raw('sum(obtain_marks)'))->with(array('Subject' => function($subject){
    $subject->orderby('subject_name')->select(DB::raw('count(subjects.id)','sum(total_marks)','subject_name'));
        }));
    }))->where('id', $id)->get();

为什么这没有执行?

它在mysql中工作 select count(subjects.id),sum(subjects.total_marks),sum(marks.obtain_marks),subjects.subject_name from students 内部连接标记 students.id = 标记.student_id 内部连接主题 subject.id = 标记.subject_id 其中student.id = 20 按subjects.subject_name分组;它在mysql中工作。

【问题讨论】:

标签: php laravel eloquent


【解决方案1】:

在选择中你必须选择你有本地键的列

$records = Student::with(['Mark' => function($marks){
    $marks->select(DB::raw('sum(obtain_marks)'), 'Subject_id' /* same Problem here ->select('subject_id')*/)->with(['Subject' => function($subject){
        $subject->orderby('subject_name')
                ->select(DB::raw('count(subjects.id)','sum(total_marks)','subject_name'));
    }]);
}])
->where('id', $id)
->select('Mark_id') // you must select foreign key column to use relationship
->get();

【讨论】:

  • 但我不知道附加外键的数据库列的名称
  • @JonasStaudenmeir 你有同样的问题吗?
  • 我想检查一下这个问题是否可以解决或者OP仍然需要帮助。
猜你喜欢
  • 2019-12-06
  • 1970-01-01
  • 2015-02-27
  • 2013-10-03
  • 2017-07-19
  • 1970-01-01
  • 2020-11-02
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多