Laravel-选择从当前日期起两个月的目的地计数
我将Larave-5.8用于Web应用程序。我已经有了下面的代码Laravel-选择从当前日期起两个月的目的地计数,laravel,eloquent,Laravel,Eloquent,我将Larave-5.8用于Web应用程序。我已经有了下面的代码 $destination = Trip::select('destination') ->selectRaw('COUNT(*) AS count') ->groupBy('destination') ->orderByDesc('count') ->limit(5) ->get(); 如何修改代码以获取从当前日期算起的过去两个月的行程目的地计数?假设您的查询已经运行,
$destination = Trip::select('destination')
->selectRaw('COUNT(*) AS count')
->groupBy('destination')
->orderByDesc('count')
->limit(5)
->get();
如何修改代码以获取从当前日期算起的过去两个月的行程目的地计数?假设您的查询已经运行,您可以使用
whereDate
方法获取该值。。像这样:
$destination = Trip::select('destination')
->selectRaw('COUNT(*) AS count')
->groupBy('destination')
->orderByDesc('count')
->whereDate('created_at', '>', now()->subMonths(2))
->limit(5)
->get();
或者,如果您希望未来2个月,请使用addMonths(2)
->其中('created_at','>',(new\Carbon\Carbon)->subMonths(2)),而不是subMonths
;