【问题标题】:Retrieving Only last years data with count - Carbon [Laravel]使用计数检索仅去年的数据 - Carbon [Laravel]
【发布时间】:2017-12-13 14:30:15
【问题描述】:

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

目前我有,

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

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

我怎样才能只获得去年的数据?必须是动态的。

【问题讨论】:

    标签: php laravel laravel-5 laravel-4 laravel-5.3


    【解决方案1】:

    Laravel 的查询构建器有一个whereYear() 函数:

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

    【讨论】:

      【解决方案2】:

      另一种方法是使用whereBetween()

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

      【讨论】:

      • 我不喜欢这种方法。 whereYear() 方法是专门为这些场景制作的。
      • 是的,它非常适合这种情况,直到现在才知道。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2016-04-30
      相关资源
      最近更新 更多