1

In my Laravel project, I have a Match model which contains the following fields: id and starting_at timestamp.

I would like to list all matches and group them by date, just like this:

1st of June:

  1. Match id 57
  2. Match id 87

2nd of June:

  1. Match id 40
  2. Match id 99

...

Thanks !

1
  • Look here. Commented May 20, 2014 at 20:06

2 Answers 2

3

You need to return a collection and then group items by starting_at:

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

Mind that SQL timestamp is not a valid array key, so if starting_at is such a field, then you need to change it a bit, for example:

$matchesByDate = $matches->groupBy(function ($match) { return substr($match->starting_at, 0, 10);});
Sign up to request clarification or add additional context in comments.

Comments

0

this is not tested but should work....also you should probably split this up into a one to many relationship that would make this much easier.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.