Search code examples
laravellaravel-5.1laravel-collection

Laravel 5.1 How to sort by


Is it possible to sort a eager collection based on the value of a relationship in an associated model?

Matches

$matches = Match::where('tournament_id', $tournamentId)->with([
    'playingMatch', 'playingMatch.court', 'playingMatch.playingSets', 'player', 'opponent'
])->get();

I have a list of matches that I use $matche->each in order to isolate the different states the different matches may be in:

Isolating Match States

$upcomingMatches = collect([]);
$currentMatches = collect([]);
$finishedMatches = collect([]);

// Isolate all upcoming, current, and completed matches
$matches->each(function ($match) use ($upcomingMatches, $currentMatches, $finishedMatches) {

    // Has the match been started?
    if (!is_null($match->playingMatch)) {

        // Is the match finished?
        if ($match->playingMatch->finished) {
            $finishedMatches->push($match);
        }
        else {
            $currentMatches->push($match);
        }
    }
    else {
        $upcomingMatches->push($match);
    }
});

Example JSON

array:3 [▼
  0 => array:9 [▼
    "id" => 1
    "tournament_id" => 2
    "player_id" => 1
    "opponent_id" => 2
    "title" => "Quarter Finals"
    "scheduled_start" => "Nov 6, 2015 7:14 pm"
    "playing_match" => array:10 [▼
      "id" => 1
      "match_id" => 1
      "court_id" => 1
      "score_player" => 2
      "score_opponent" => 0
      "start_time" => "2015-11-06 11:14:36"
      "finish_time" => "2015-11-06 11:14:57" <-- sort argument
      "finished" => true
      "court" => array:5 [▶]
      "playing_sets" => array:2 [▶]
    ]
    "player" => array:5 [▶]
    "opponent" => array:5 [▶]
  ]
  1 => array:9 [▶]
  2 => array:9 [▶]
]

But now that I have all the $finishedMatches I want them ordered by finish_time, which is located in $finishedMatches[0]->playingMatch->finish_time. I can't seem to figure out a way I could do this using Laravel collections. Any ideas?


Solution

  • I believe this would do the trick:

    $finishedMatches->sortBy(function($match){
        return $match->playingMatch->finish_time;
    });
    

    I would also probably use filters instead of an each to create the collections.

    $finishedMatches = $matches->filter(function ($match) {
        return ($match->playingMatch && $match->playingMatch->finished);
    });
    
    $currentMatches = $matches->filter(function ($match) {
        return !($match->playingMatch && $match->playingMatch->finished);
    });
    
    $upcomingMatches = $matches->filter(function ($match) {
        return !($match->playingMatch);
    });
    

    It's a little easier on my eyes :)