Laravel 5.3循环错误

时间:2016-11-11 09:08:07

标签: for-loop laravel-5.3

嗨我试着循环几个月和几年的日历但是我的循环在索引函数的第一次迭代时停止你能帮助我吗?

索引功能:

public function index()
{
$users = DB::table('users')->where('roles_id', '2')->get();

  date_default_timezone_set("Asia/Hong_Kong");

  for ($iterateYear = 2017; $iterateYear < 2018; $iterateYear++) {
    for ($iterateMonth = 1; $iterateMonth <= 12; $iterateMonth++) {

      /* Set the date */
      $date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
      return $this->renderCalenderMonth($date);

    }
  }

    return view('transaction.index', [
      'users' => $users
    ]);
}

RenderCalendarMonth函数:

公共函数renderCalenderMonth($ date){

  // $date = strtotime(date("Y-m-d"));
  $NUMBER_OF_COLUMNS = 37;

  $day = date('d', $date);
  $month = date('m', $date);
  $year = date('Y', $date);

  $firstDay = mktime(0,0,0,$month, 1, $year);
  $title = strftime('%B', $firstDay);
  $dayOfWeek = date('D', $firstDay);
  $daysInMonth = cal_days_in_month(0, $month, $year);
  /* Get the name of the week days */
  $timestamp = strtotime('next Sunday');
  $weekDays = [];

  for($i = 0; $i < $NUMBER_OF_COLUMNS; $i++) {
      $weekDays[] = strftime('%a', $timestamp);
      $timestamp = strtotime('+1 day', $timestamp);
  }
  $blank = date('w', strtotime("{$year}-{$month}-01"));

  $divid = $title.$year;

  return view('transaction.index', [
    'date' => $date,
    'day' => $day,
    'month' => $month,
    'year' => $year,
    'firstDay' => $firstDay,
    'title' => $title,
    'dayOfWeek' => $dayOfWeek,
    'daysInMonth' => $daysInMonth,
    'timestamp' => $timestamp,
    'weekDays' => $weekDays,
    'blank' => $blank,
    'divid' => $divid,
    'NUMBER_OF_COLUMNS' => $NUMBER_OF_COLUMNS
  ]);

}

1 个答案:

答案 0 :(得分:1)

你的循环中有一个return语句,所以你要做的就是进入renderCalenderMonth然后返回生成的视图。然后离开循环。如果你想连续从循环中返回值,请使用php yield关键字。