是否可以根据关联模型中关系的值对急切集合进行排序?
匹配
$matches = Match::where('tournament_id', $tournamentId)->with([
'playingMatch', 'playingMatch.court', 'playingMatch.playingSets', 'player', 'opponent'
])->get();
我有一个匹配列表,我使用$matche->each
来隔离不同匹配的不同状态:
隔离匹配状态
$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);
}
});
示例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 [▶]
]
但是现在我已经拥有$finishedMatches
所有finish_time
我想要的$finishedMatches[0]->playingMatch->finish_time
,@api.multi
@api.depends('name') # seems to depend on record.name ??
def _compute_fats(self):
for record in self:
fats = 0
fats_id = record.name.id # Are you sure this is correct??
get_detail = self.env['trackfood.foods'].browse([fats_id]) # Faster search by id
for record2 in get_detail:
fats = record2.fats
record.fats = fats
位于{{1}}。我似乎无法通过Laravel系列找到一种方法。有什么想法吗?
答案 0 :(得分:4)
我相信这会解决问题:
$finishedMatches->sortBy(function($match){
return $match->playingMatch->finish_time;
});
我也可能会使用过滤器而不是每个过滤器来创建集合。
$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);
});
我的眼睛有点容易:)