Laravel-显示最近30天的用户 [英] Laravel - Show users from the last 30 days
本文介绍了Laravel-显示最近30天的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的Laravel查询
I have a Laravel query that looks like this
$users = DB::table("users")
->select('id')
->where('accounttype', 'standard')
->get()->all();
它正在工作并返回所有标准帐户的ID,我试图将其限制为仅返回过去30天(创建用户的日期作为时间戳存储在"created_at"中)的结果
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?
推荐答案
您可以将碳与 whereDate
子句一起使用:
You can use carbon along with the whereDate
clause:
use Carbon\Carbon;
$users = DB::table("users")
->select('id')
->where('accounttype', 'standard')
->whereDate('created_at', '>', Carbon::now()->subDays(30))
->all();
如注释中所述,请在查询中进行尽可能多的操作,直到发现性能问题或查询变得不可读为止.
As noted in the comments, do as much in the query as possible until you notice performance issues or your queries become unreadable.
这篇关于Laravel-显示最近30天的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文