@@ -116,19 +116,19 @@ public function getLatestArrivalsForUser(User $user, int $maxCount = 5): Collect
116116 {
117117 $ latestStations = DB ::table ('train_checkins ' )
118118 ->join ('train_stopovers ' , 'train_checkins.destination_stopover_id ' , '= ' , 'train_stopovers.id ' )
119- ->join ('train_stations ' , 'train_stopovers.train_station_id ' , '= ' , 'train_stations.id ' )
120119 ->where ('train_checkins.user_id ' , $ user ->id )
121- ->groupBy ('train_stations.id ' )
122- ->select (['train_stations.id ' , DB ::raw ('MAX(train_checkins.arrival) as last_arrival ' )])
123- ->orderByDesc (DB ::raw ('MAX(train_checkins.arrival) ' ))
124- ->limit ($ maxCount )
125- ->get ();
120+ ->orderByDesc ('train_checkins.arrival ' )
121+ ->select (['train_stopovers.train_station_id ' , 'train_checkins.arrival ' ])
122+ ->limit ($ maxCount * 10 )
123+ ->get ()
124+ ->unique ('train_station_id ' )
125+ ->take ($ maxCount );
126126
127127 return Station::with (['areas ' , 'stationIdentifiers ' ])
128- ->whereIn ('id ' , $ latestStations ->pluck ('id ' ))
128+ ->whereIn ('id ' , $ latestStations ->pluck ('train_station_id ' ))
129129 ->get ()
130130 ->sortBy (function (Station $ station ) use ($ latestStations ) {
131- return $ latestStations ->firstWhere ('id ' , $ station ->id )->last_arrival ;
131+ return $ latestStations ->firstWhere ('train_station_id ' , $ station ->id )->arrival ;
132132 }, SORT_REGULAR , true );
133133 }
134134}
0 commit comments