Sum of the values of a particular user for a particular month of a particular year
I have a table with time stamp, user id, value. I am trying to get sum of the values of a particular user for a particular month of a particular year. This is what I am using:
$data = Laborhrs::all()->where('employee','7')->sum('value');
This is giving me sum of whole data for employee#7. I am looking to get sum of a particular month of a particular year. Say Sum of values of October 2018. What am I missing?
laravel
add a comment |
I have a table with time stamp, user id, value. I am trying to get sum of the values of a particular user for a particular month of a particular year. This is what I am using:
$data = Laborhrs::all()->where('employee','7')->sum('value');
This is giving me sum of whole data for employee#7. I am looking to get sum of a particular month of a particular year. Say Sum of values of October 2018. What am I missing?
laravel
employee is timestamp column ?
– Kul
Jan 3 at 10:31
Not the answer to your problem, but important nonetheless:all()
method will issue a query to DB and return you a collection. So yourwhere(...)
call goes on the collection object and it's not the best practice - it's much more efficient to delegate it to your DB side. Same goes for aggregating functions (such assum(...)
in your example since it's also featured in query builder object). Selecting everything from DB and only then operating on collection of objects is very bad and can drastically slow your script execution time while also increasing memory consumption.
– d3jn
Jan 3 at 10:38
add a comment |
I have a table with time stamp, user id, value. I am trying to get sum of the values of a particular user for a particular month of a particular year. This is what I am using:
$data = Laborhrs::all()->where('employee','7')->sum('value');
This is giving me sum of whole data for employee#7. I am looking to get sum of a particular month of a particular year. Say Sum of values of October 2018. What am I missing?
laravel
I have a table with time stamp, user id, value. I am trying to get sum of the values of a particular user for a particular month of a particular year. This is what I am using:
$data = Laborhrs::all()->where('employee','7')->sum('value');
This is giving me sum of whole data for employee#7. I am looking to get sum of a particular month of a particular year. Say Sum of values of October 2018. What am I missing?
laravel
laravel
asked Jan 3 at 10:25
ThahaThaha
183110
183110
employee is timestamp column ?
– Kul
Jan 3 at 10:31
Not the answer to your problem, but important nonetheless:all()
method will issue a query to DB and return you a collection. So yourwhere(...)
call goes on the collection object and it's not the best practice - it's much more efficient to delegate it to your DB side. Same goes for aggregating functions (such assum(...)
in your example since it's also featured in query builder object). Selecting everything from DB and only then operating on collection of objects is very bad and can drastically slow your script execution time while also increasing memory consumption.
– d3jn
Jan 3 at 10:38
add a comment |
employee is timestamp column ?
– Kul
Jan 3 at 10:31
Not the answer to your problem, but important nonetheless:all()
method will issue a query to DB and return you a collection. So yourwhere(...)
call goes on the collection object and it's not the best practice - it's much more efficient to delegate it to your DB side. Same goes for aggregating functions (such assum(...)
in your example since it's also featured in query builder object). Selecting everything from DB and only then operating on collection of objects is very bad and can drastically slow your script execution time while also increasing memory consumption.
– d3jn
Jan 3 at 10:38
employee is timestamp column ?
– Kul
Jan 3 at 10:31
employee is timestamp column ?
– Kul
Jan 3 at 10:31
Not the answer to your problem, but important nonetheless:
all()
method will issue a query to DB and return you a collection. So your where(...)
call goes on the collection object and it's not the best practice - it's much more efficient to delegate it to your DB side. Same goes for aggregating functions (such as sum(...)
in your example since it's also featured in query builder object). Selecting everything from DB and only then operating on collection of objects is very bad and can drastically slow your script execution time while also increasing memory consumption.– d3jn
Jan 3 at 10:38
Not the answer to your problem, but important nonetheless:
all()
method will issue a query to DB and return you a collection. So your where(...)
call goes on the collection object and it's not the best practice - it's much more efficient to delegate it to your DB side. Same goes for aggregating functions (such as sum(...)
in your example since it's also featured in query builder object). Selecting everything from DB and only then operating on collection of objects is very bad and can drastically slow your script execution time while also increasing memory consumption.– d3jn
Jan 3 at 10:38
add a comment |
1 Answer
1
active
oldest
votes
You forgot to add a condition for the year and month, and you don't need to use all()
method.
$sum = Laborhrs::where('employee','7')
->whereMonth('timestamp_column_name', 10)
->whereYear('timestamp_column_name', 2018)
->sum('value');
In addition: I saw your several posts, and I think, you don't understand how laravel eloquent and collections work.
All()
method returns collection with all results from database, and then you filters them trough collection (I saw it not first time). It is very slow solution.
Better to use conditions with get()
(get()
should go after conditions), for example:
$data = Laborhrs::where('employee','7')->get();
It will return collection with results that you need, and you don't need to filter them after getting from database. It will be work much faster.
In that situtation you don't need all()
and get()
methods at all. Eloquent has sum()
method, that will calculate sum in database and return value.
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f54020388%2fsum-of-the-values-of-a-particular-user-for-a-particular-month-of-a-particular-ye%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You forgot to add a condition for the year and month, and you don't need to use all()
method.
$sum = Laborhrs::where('employee','7')
->whereMonth('timestamp_column_name', 10)
->whereYear('timestamp_column_name', 2018)
->sum('value');
In addition: I saw your several posts, and I think, you don't understand how laravel eloquent and collections work.
All()
method returns collection with all results from database, and then you filters them trough collection (I saw it not first time). It is very slow solution.
Better to use conditions with get()
(get()
should go after conditions), for example:
$data = Laborhrs::where('employee','7')->get();
It will return collection with results that you need, and you don't need to filter them after getting from database. It will be work much faster.
In that situtation you don't need all()
and get()
methods at all. Eloquent has sum()
method, that will calculate sum in database and return value.
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
add a comment |
You forgot to add a condition for the year and month, and you don't need to use all()
method.
$sum = Laborhrs::where('employee','7')
->whereMonth('timestamp_column_name', 10)
->whereYear('timestamp_column_name', 2018)
->sum('value');
In addition: I saw your several posts, and I think, you don't understand how laravel eloquent and collections work.
All()
method returns collection with all results from database, and then you filters them trough collection (I saw it not first time). It is very slow solution.
Better to use conditions with get()
(get()
should go after conditions), for example:
$data = Laborhrs::where('employee','7')->get();
It will return collection with results that you need, and you don't need to filter them after getting from database. It will be work much faster.
In that situtation you don't need all()
and get()
methods at all. Eloquent has sum()
method, that will calculate sum in database and return value.
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
add a comment |
You forgot to add a condition for the year and month, and you don't need to use all()
method.
$sum = Laborhrs::where('employee','7')
->whereMonth('timestamp_column_name', 10)
->whereYear('timestamp_column_name', 2018)
->sum('value');
In addition: I saw your several posts, and I think, you don't understand how laravel eloquent and collections work.
All()
method returns collection with all results from database, and then you filters them trough collection (I saw it not first time). It is very slow solution.
Better to use conditions with get()
(get()
should go after conditions), for example:
$data = Laborhrs::where('employee','7')->get();
It will return collection with results that you need, and you don't need to filter them after getting from database. It will be work much faster.
In that situtation you don't need all()
and get()
methods at all. Eloquent has sum()
method, that will calculate sum in database and return value.
You forgot to add a condition for the year and month, and you don't need to use all()
method.
$sum = Laborhrs::where('employee','7')
->whereMonth('timestamp_column_name', 10)
->whereYear('timestamp_column_name', 2018)
->sum('value');
In addition: I saw your several posts, and I think, you don't understand how laravel eloquent and collections work.
All()
method returns collection with all results from database, and then you filters them trough collection (I saw it not first time). It is very slow solution.
Better to use conditions with get()
(get()
should go after conditions), for example:
$data = Laborhrs::where('employee','7')->get();
It will return collection with results that you need, and you don't need to filter them after getting from database. It will be work much faster.
In that situtation you don't need all()
and get()
methods at all. Eloquent has sum()
method, that will calculate sum in database and return value.
edited Jan 3 at 10:52
answered Jan 3 at 10:34
IndianCodingIndianCoding
1,0441110
1,0441110
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
add a comment |
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
Thank you for the additional explanation. I have noted your input and trying to understand it.
– Thaha
Jan 3 at 11:40
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.
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%2f54020388%2fsum-of-the-values-of-a-particular-user-for-a-particular-month-of-a-particular-ye%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
employee is timestamp column ?
– Kul
Jan 3 at 10:31
Not the answer to your problem, but important nonetheless:
all()
method will issue a query to DB and return you a collection. So yourwhere(...)
call goes on the collection object and it's not the best practice - it's much more efficient to delegate it to your DB side. Same goes for aggregating functions (such assum(...)
in your example since it's also featured in query builder object). Selecting everything from DB and only then operating on collection of objects is very bad and can drastically slow your script execution time while also increasing memory consumption.– d3jn
Jan 3 at 10:38