[英]laravel use closure with relationship
Assume I have a Group
and Student
model, it's a one-to-many relationship; 假设我有一个“
Group
和Student
模型,这是一对多关系;
Group
has many Student
,every Student
has id
and tuition
attribute. Group
有很多Student
,每个Student
都有id
和tuition
属性。
So I want get Group with students numbers and all the tuition . 所以我要和学生人数及所有学费一起分组。
Here is my code: 这是我的代码:
Group::with(['student'=>function($query){
$query->select(DB::raw('count(`id`) as numbers, sum(tuition) as total'));
}])->paginate(10);
It's not working,I tried print the sql, and the sql: 它不起作用,我尝试打印sql和sql:
select count(id) as numbers, sum(tuition) as total from `group` where `student`.`group_id` in (`1`, `2`, `4`, `5`, `6`, `7`, `8`, `11`, `12`, `13`, `14`)
I can get results when run raw sql in mysql, but laravel doesn't return anything about count
or sum
. 在mysql中运行原始sql时,我可以得到结果,但是laravel不返回有关
count
或sum
任何信息。
Use withCount()
instead of with()
: 使用
withCount()
代替with()
:
Group::withCount([
'student as numbers',
'student as total' => function($query) {
$query->select(DB::raw('sum(tuition)'));
}
])->paginate(10);
Solution for Laravel 5.2: Laravel 5.2的解决方案:
Group::selectRaw('(select count(*) from students where groups.id = students.group_id) as numbers')
->selectRaw('(select sum(tuition) from students where groups.id = students.group_id) as total')
->paginate(10);
You can use withCount()
instead of with()
您可以使用
withCount()
代替with()
https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models
Tested a lot; 经过大量测试;
When I use find
get only one row. 当我使用
find
仅获得一行。
Group::with(['student'=>function($query){
$query->select(DB::raw(' group_id ,count(`id`) as number, sum(tuition) as total'));
}])->find(1);
It worked. 有效。
The only thing I miss is I need select student.group_id
,which means foreign key
in hasMany
relationship. 我唯一想念的是我需要选择
student.group_id
,这意味着hasMany
关系中的foreign key
。
But when you want use paginate
or get
method fetch multiply rows. 但是,当您要使用
paginate
或get
方法获取多行时。
You will only get a total result in your first model object with others are null. 您将仅在第一个模型对象中获得总计结果,而其他对象为null。
{
"id": 1,
"name":"first",
"student": [
{
"group_id": 1,
"number": 129,
"total": "38700.00"
}
]
},
{
"id": 2,
"name":"second",
"student": []
},
{
"id": 3,
"name":"third",
"student": []
},
Just add ->groupBy('group_id)
and you will get what you want 只需添加
->groupBy('group_id)
,您将得到想要的
Group::with(['student'=>function($query){
$query->select(DB::raw('id, class_id ,count(`id`) as numbers, sum(tuition) as total'))->groupBy('group_id');
}])->paginate(10);
Result: 结果:
{
"id": 1,
"name":"first",
"student": [
{
"group_id": 1,
"number": 40,
"total": "12000.00"
}
]
},
{
"id": 2,
"name":"second",
"student": [
{
"group_id": 2,
"number": 43,
"total": "12900.00"
}
]
},
{
"id": 3,
"name":"third",
"student": [
{
"group_id": 3,
"number": 46,
"total": "13800.00"
}
]
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.