仅检索带有计数的去年数据 - Carbon [Laravel]

Retrieving Only last years data with count - Carbon [Laravel]

我正在制作一个计数器,仅检索 2017 年的去年数据。仅从 1 月到 12 月。我的下一个功能是 subYears(1) 以获取前几年的数据。

目前我有,

  $intake = Intake::where('confirmed', 1)
                      ->where('officiant_id', $this->id)
                       ->whereIn('weddingStartDateTime', Carbon::now()->format('Y') )
                      ->get();

(get 将替换为 ->count 我只是想显示它用于测试目的。

如何只获取去年的数据?必须是动态的。

Laravel 的查询生成器有一个 whereYear() 函数:

$intake = Intake::where('confirmed', 1)
        ->where('officiant_id', $this->id)
        ->whereYear('weddingStartDateTime', Carbon::now()->format('Y'))
        ->get();

另一种方法是使用 whereBetween()

例如Intake::whereBetween('weddingStartDateTime',[$date1,$date2]);