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?
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.
This works for me
$thirty_days_ago = date('Y-m-d',strtotime("-31 days"));
$users_thirty_days_ago = User::where('accounttype', 'standard')->whereDate('last_online', ">=" , $thirty_days_ago)->count();
You can use it
\App\Model::whereBetween('date', [now()->subdays(30), now()->subday()])->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With