2014-05-20 3 views
1

내 Laravel 프로젝트에서 모델에 idstarting_at 타임 스탬프가 포함 된 모델이 있습니다.Laravel : 날짜별로 그룹 결과

난 그냥 다음과 같이 날짜별로 모든 일치 및 그룹을 나열하고 싶습니다 : 6 월

1 :

  1. 일치 ID 57
  2. 일치 ID 87

6 월 2 일 :

    ,
  1. 일치 ID 40
  2. 일치 ID 99

...

감사합니다!

+0

[여기] (http://laravel.com/docs/queries#selects)를 참조하십시오. –

답변

3

당신은 starting_at에 의해 다음 수집 및 그룹 항목을 반환해야합니다 SQL 타임 스탬프가 유효한 배열의 키가 아닌

$matches = Match::all(); // or whatever constraints you want to apply 
$matchesByDate = $matches->groupBy('starting_at'); 

마음 것을, 그래서 starting_at 같은 필드가있는 경우, 당신은 필요 예를 들면 다음과 같습니다.

$matchesByDate = $matches->groupBy(function ($match) { return substr($match->starting_at, 0, 10);}); 
0

이것은 테스트되지는 않았지만 작동해야합니다. 또한이 문제를 훨씬 쉽게 이해할 수있는 일대 다 관계로 분리해야합니다.

$matches = Matches::select('starting_at')->distinct()->get(); 
    foreach($matches as $match){ 
     echo $match->starting_at."<br/>"; 
     $ids = Matches::where("starting_at",$match->starting_at)->lists('id'); 
     $count = 1; 
     foreach ($ids as $id){ 
     echo $count.". Match id ".$id."<br/>"; 
     $count ++; 
     } 
    }