php - 在关系上选择查询未在 laravel eloquent 中给出结果
问题描述
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 内部连接subjects.id = mark.subject_id where students.id = 20 group by subject.subject_name; 它在mysql中工作。
解决方案
在 Select 您必须选择您拥有本地密钥的列
$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();
推荐阅读
- c# - 网页上的按钮不可点击
- java - JPA 试图强迫我拥有一个名为“id”的属性和数据库列
- ionic3 - Ionic 3 - 启动画面大小
- apache-kafka - Grafana/prometheus 中没有 kafka 指标
- google-apps-script - 简化这段代码?从文件夹及其子文件夹中获取所有文件——当前代码超时
- javascript - Power BI 嵌入退出全屏
- java - Spring Boot OAuth2:从 cookie 中提取 JWT 进行身份验证
- html - 如何在 Jekyll 中渲染 {%%} 的 Markdown?
- ios - Swift 4 如何在文档目录中写入/读取字典?
- kubernetes - k8s Prometheus:pod 有未绑定的 PersistentVolumeClaims