周开始的群体活动在Eloquent

时间:2013-05-11 10:25:03

标签: grouping laravel laravel-4 eloquent

我的事件模型中有以下方法用于我的事件:

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')
            );
        }
    }

编辑:好的,我现在在我的控制器中有这个。它越来越接近,但还不是很正确。

    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;
    }

1 个答案:

答案 0 :(得分:0)

好的,所以我现在用以下代码解决了我的问题:

public function index()
{
    $events = Event::getEvents($perPage = 10);

    $events_by_week = array();

    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_by_week))
        {
            $events_by_week[$week] = (object) array(
                'week'      => $week_commencing->format('jS F Y'),
                'events'    => array()
            );
        }

        $events_by_week[$week]->events[] = $event;
    }

    $this->layout->content = View::make('events.index')->with(array('events_by_week' => $events_by_week, 'events' => $events));
}