Issue
I have a Laravel query that looks like this
$users = DB::table("users")
->select('id')
->where('accounttype', 'standard')
->get()->all();
It is working and returning the id for all standard accounts, I am trying to limit it to only return results from the last 30 days, the date the user was created is stored as a timestamp in 'created_at'
Is it best to do this in the query or should I process the results afterwards?
Solution
You can use carbon along with the where clause:
use Carbon\Carbon;
$users = DB::table("users")
->select('id')
->where('accounttype', 'standard')
->where('created_at', '>', now()->subDays(30)->endOfDay())
->all();
As noted in the comments, do as much in the query as possible until you notice performance issues or your queries become unreadable.
Answered By - Chris
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.