laravel use closure with relationship
up vote
0
down vote
favorite
Assume I have a Group
and Student
model, it's a one-to-many relationship;
Group
has many Student
,every Student
has id
and tuition
attribute.
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:
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
.
laravel eloquent
add a comment |
up vote
0
down vote
favorite
Assume I have a Group
and Student
model, it's a one-to-many relationship;
Group
has many Student
,every Student
has id
and tuition
attribute.
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:
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
.
laravel eloquent
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Assume I have a Group
and Student
model, it's a one-to-many relationship;
Group
has many Student
,every Student
has id
and tuition
attribute.
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:
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
.
laravel eloquent
Assume I have a Group
and Student
model, it's a one-to-many relationship;
Group
has many Student
,every Student
has id
and tuition
attribute.
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:
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
.
laravel eloquent
laravel eloquent
asked Nov 9 at 1:07
Evol Rof
1841114
1841114
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Use withCount()
instead of with()
:
Group::withCount([
'student as numbers',
'student as total' => function($query)
$query->select(DB::raw('sum(tuition)'));
])->paginate(10);
Solution for 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);
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.
– Jonas Staudenmeir
Nov 9 at 1:53
add a comment |
up vote
0
down vote
You can use withCount()
instead of with()
https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models
add a comment |
up vote
0
down vote
Tested a lot;
When I use find
get only one row.
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.
But when you want use paginate
or get
method fetch multiply rows.
You will only get a total result in your first model object with others are 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
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"
]
,
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Use withCount()
instead of with()
:
Group::withCount([
'student as numbers',
'student as total' => function($query)
$query->select(DB::raw('sum(tuition)'));
])->paginate(10);
Solution for 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);
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.
– Jonas Staudenmeir
Nov 9 at 1:53
add a comment |
up vote
0
down vote
accepted
Use withCount()
instead of with()
:
Group::withCount([
'student as numbers',
'student as total' => function($query)
$query->select(DB::raw('sum(tuition)'));
])->paginate(10);
Solution for 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);
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.
– Jonas Staudenmeir
Nov 9 at 1:53
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Use withCount()
instead of with()
:
Group::withCount([
'student as numbers',
'student as total' => function($query)
$query->select(DB::raw('sum(tuition)'));
])->paginate(10);
Solution for 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);
Use withCount()
instead of with()
:
Group::withCount([
'student as numbers',
'student as total' => function($query)
$query->select(DB::raw('sum(tuition)'));
])->paginate(10);
Solution for 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);
edited Nov 9 at 1:52
answered Nov 9 at 1:31
Jonas Staudenmeir
11.6k2932
11.6k2932
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.
– Jonas Staudenmeir
Nov 9 at 1:53
add a comment |
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.
– Jonas Staudenmeir
Nov 9 at 1:53
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
I'm using laravel 5.2, it says call to undefined function.Any idea?
– Evol Rof
Nov 9 at 1:47
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.– Jonas Staudenmeir
Nov 9 at 1:53
withCount()
requires Laravel 5.3. I added an alternative with raw subqueries.– Jonas Staudenmeir
Nov 9 at 1:53
add a comment |
up vote
0
down vote
You can use withCount()
instead of with()
https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models
add a comment |
up vote
0
down vote
You can use withCount()
instead of with()
https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use withCount()
instead of with()
https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models
You can use withCount()
instead of with()
https://laravel.com/docs/5.5/eloquent-relationships#counting-related-models
answered Nov 9 at 2:21
Nguyen Hung Thai
1
1
add a comment |
add a comment |
up vote
0
down vote
Tested a lot;
When I use find
get only one row.
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.
But when you want use paginate
or get
method fetch multiply rows.
You will only get a total result in your first model object with others are 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
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"
]
,
add a comment |
up vote
0
down vote
Tested a lot;
When I use find
get only one row.
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.
But when you want use paginate
or get
method fetch multiply rows.
You will only get a total result in your first model object with others are 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
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"
]
,
add a comment |
up vote
0
down vote
up vote
0
down vote
Tested a lot;
When I use find
get only one row.
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.
But when you want use paginate
or get
method fetch multiply rows.
You will only get a total result in your first model object with others are 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
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"
]
,
Tested a lot;
When I use find
get only one row.
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.
But when you want use paginate
or get
method fetch multiply rows.
You will only get a total result in your first model object with others are 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
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"
]
,
answered Nov 9 at 2:38
Evol Rof
1841114
1841114
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218398%2flaravel-use-closure-with-relationship%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown