2015-03-12 3 views
0

Мне нужно сгруппировать таблицу днем ​​и временем с laravel. я получил объект массива, где я выбрать какую-либо информацию (контроллер):таблица группа день laravel

public function getNotification() 
{ 

    $message  = Auth::user()->Notifications()->orderBy('created_at', 'desc')->get(); 

    $this->layout->content = View::make('auth/main')->with(array(
     'data'  => View::make('auth/dashboard')->with([ 
      'message' => $message 
     ]), 
     'fluid' => true 
    )); 
} 

это мой DB:

id/message/name/created_at 
1 some text announcement 2015-03-11 10:24:05 
2 some text announcement 2015-03-11 08:24:05 
2 some text announcement 2015-03-10 14:24:05 
4 some text announcement 2015-03-08 12:24:05 

это лезвие TMPL:

<table class="table notifications"> 
 
    <thead> 
 
    <tr> 
 
     <th colspan="3">YESTERDAY</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td class="date">8:59 am</td> 
 
     <td class="icon"><span class="info-red"></span> 
 
     </td> 
 
     <td><strong>Announcement</strong>: As business grows, we all need to adapt. You've been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design. You've been growing, and receiving 
 
     bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design.You've been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design.You've 
 
     been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design. 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td class="date">8:59 am</td> 
 
     <td class="icon"><span class="info-green"></span> 
 
     </td> 
 
     <td><strong>Announcement</strong>: As business grows, we all need to adapt. You've been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design. You've been growing, and receiving 
 
     bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design.You've been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design.You've 
 
     been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design. 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<table class="table notifications"> 
 
    <thead> 
 
    <tr> 
 
     <th colspan="3">FEBRUARY 06</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td class="date">8:59 am</td> 
 
     <td class="icon"><span class="info-red"></span> 
 
     </td> 
 
     <td><strong>Announcement</strong>: As business grows, we all need to adapt. You've been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design. You've been growing, and receiving 
 
     bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design.You've been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design.You've 
 
     been growing, and receiving bigger amounts of orders. We've noticed the need for printing high volumes of the same kind of design. 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table>

и мне нужно результат, как у моего лезвия:

1 и 2 ID из db в одной таблице, потому что они являются сегодня группой, 3 id во второй таблице, 4 id в третьей таблице

Как сгруппировать таблицу по дням и как она должна выглядеть, как мой лезвие tmpl с foreach, возможно?

+0

Вы хотите, например, все сообщения со вчерашнего дня? Может, не нужна группа? – wiesson

+0

Мне нужна группа: если сообщения на вчерашней дате, чем в лезвии разобрать html их из одной таблицы. если массажем является другая дата, затем проанализируйте другую таблицу, но не все сообщения в одной таблице. это должно выглядеть как foreach в другом foreach, возможно – Haroldas

+0

Да, но вы можете получать сообщения на основе даты. Чтобы найти все сообщения от вчера -> '' 'где ('created_at'> $ date_yesterday) -> где ('created_at' <= $ date_yesterday)' ''. – wiesson

ответ

0

Попробуйте это ..

$message = Auth::user()->Notifications()->orderBy('created_at', 'desc')->groupBy('created_at')->get(); 
+0

, спасибо, что это работает. мой код: '$ message = Auth :: user() -> Уведомления() -> orderBy ('created_at', 'desc') -> get() -> groupBy (function ($ date) { return Carbon :: parse ($ date-> created_at) -> format ('M d'); // группировка по дням }); ' как он разбивается на страницы? – Haroldas

+0

обратитесь по этой ссылке: http: //culttt.com/2014/02/24/working-pagination-laravel-4/ –

0

проблема решена:

$message = Auth::user()->Notifications()->orderBy('created_at', 'desc')->get() 
       ->groupBy(function($date) { 
        return Carbon::parse($date->created_at)->format('M d'); // grouping by day 
       }); 

как постраничной?

Смежные вопросы