【问题标题】:Please tell me why an empty array is written to the variables请告诉我为什么将空数组写入变量
【发布时间】:2019-04-01 10:42:27
【问题描述】:
$January = $articles->whereMonth('updated_at','01')
  ->whereYear('updated_at', $year)
  ->get();

$February = $articles->whereMonth('updated_at','02')
  ->whereYear('updated_at', $year)
  ->get();

如果$January 中没有数据,则无论数据可用性如何,都会将一个空数组写入$February。告诉我如何解决它以及为什么会这样?

【问题讨论】:

    标签: php mysql laravel web


    【解决方案1】:

    之前有类似的question回答过

    您需要像这样在$articles 上使用clone

    $January = (clone $articles)->whereMonth('updated_at','01')
      ->whereYear('updated_at', $year)
      ->get();
    
    $February = (clone $articles)->whereMonth('updated_at','02')
      ->whereYear('updated_at', $year)
      ->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2019-09-14
      • 2010-11-15
      • 1970-01-01
      • 2020-01-04
      • 2010-12-07
      相关资源
      最近更新 更多