イベントのイベントモデルに次のメソッドがあります。
public static function getEvents($perPage = 10)
{
$events = Event::where('active', '=', 1)->paginate($perPage);
return $events;
}
各イベントには start_date があり、その開始日を取得して、各イベントが発生する週の月曜日のように、週ごとに結果をビューにグループ化する必要があります。コントローラーにこれがあります:
public function index()
{
$events = Event::getEvents($perPage = 5);
$this->layout->content = View::make('events.index')->with('events', $events);
}
これらのイベントを週ごとにグループ化するのを手伝ってくれる人はいますか? ありがとう。
次を使用して週の開始日を取得するという点で、私は途中です:
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
$week_commencing = $week_commencing->format('jS F Y');
}
これを使用してイベントをグループ化する必要がありますが、その方法がわかりません。
編集:解決策に近づいていると感じていますが、まだ少し助けが必要です。私は今、私のコントローラーに以下を持っています.1週間の開始からすべてのイベントを出力しますが、var_dump(ing) $events->weeks_commencing の場合、オブジェクトには日付が1つしかないため、ほぼ完了です。ポインタはありますか?:
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
if (!array_key_exists($week, $events))
{
$events->weeks_commencing = (object) array(
'week' => $week_commencing->format('jS F Y')
);
}
}
編集:OK、これをコントローラーに入れました。近づいていますが、まだ完全ではありません。
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
if (array_key_exists($week, $events))
{
$events = (object) array(
'week' => $week_commencing->format('jS F Y'),
'events' => array()
);
}
$events->events[] = $event;
}