Skip to content

Commit 650cde2

Browse files
authored
🚸 Improve Autocomplete Results (#3446)
1 parent 06cdc7b commit 650cde2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

app/Http/Controllers/Backend/Transport/StationController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\DataProviders\DataProviderInterface;
77
use App\Http\Controllers\Controller;
88
use App\Models\Checkin;
9+
use App\Models\Station;
910
use App\Models\Stopover;
1011
use App\Models\User;
1112
use App\Repositories\StationRepository;
@@ -81,8 +82,12 @@ public function search(string $search): Collection {
8182
$stations = $this->dataProvider->getStations($search);
8283
if ($stations->count() < 10) {
8384
$remaining = 10 - $stations->count();
84-
$dbStations = $this->stationRepository->getStationByName($search, 'de', true, $remaining);
85-
$stations = $stations->merge($dbStations);
85+
$dbStations = $this->stationRepository->getStationByName($search, 'de', true);
86+
// remove duplicates
87+
$dbStations = $dbStations->filter(function(Station $station) use ($stations) {
88+
return !$stations->contains('id', $station->id);
89+
});
90+
$stations = $stations->merge($dbStations->take($remaining));
8691
}
8792
return $stations;
8893
}

app/Repositories/StationRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class StationRepository
1515
public function getStationByName(string $name, string $lang, bool $invertLanguage = false, int $limit = 20): Collection {
1616
$invertLanguage = $invertLanguage ? '!=' : '=';
1717

18-
return Station::leftJoin('station_names', 'station_names.station_id', '=', 'train_stations.id')
18+
return Station::with('areas')
19+
->leftJoin('station_names', 'station_names.station_id', '=', 'train_stations.id')
1920
->where('station_names.name', 'LIKE', "$name")
2021
->where('station_names.language', $invertLanguage, $lang)
2122
->orWhere('train_stations.name', 'LIKE', "$name")

0 commit comments

Comments
 (0)