diff --git a/app/Http/Controllers/CarteController.php b/app/Http/Controllers/CarteController.php index 9dfbd7f..398e161 100644 --- a/app/Http/Controllers/CarteController.php +++ b/app/Http/Controllers/CarteController.php @@ -21,8 +21,8 @@ class CarteController extends Controller ->whereHas('sources.releves') ->with([ 'sources' => function ($q) { - $q->withCount('releves') - ->select('id', 'nom', 'lieu_id', 'status', 'annee_debut', 'annee_fin') + $q->select('id', 'nom', 'lieu_id', 'status', 'annee_debut', 'annee_fin') + ->withCount('releves') ->orderBy('nom'); }, ]) diff --git a/app/Http/Controllers/ExportController.php b/app/Http/Controllers/ExportController.php index 0d8f83c..6b10955 100644 --- a/app/Http/Controllers/ExportController.php +++ b/app/Http/Controllers/ExportController.php @@ -90,7 +90,7 @@ class ExportController extends Controller $query->whereRaw('date_evenement <= ?', [$request->integer('annee_fin') . '-12-31']); } - $releves = $query->orderByRaw('nom ASC NULLS LAST')->get(); + $releves = $query->orderByRaw(DbCompat::nullsLast('nom'))->get(); if ($releves->isEmpty()) { return back()->with('error', 'Aucun relevé à exporter.'); diff --git a/app/Http/Controllers/RechercheController.php b/app/Http/Controllers/RechercheController.php index f282808..0a1e246 100644 --- a/app/Http/Controllers/RechercheController.php +++ b/app/Http/Controllers/RechercheController.php @@ -88,8 +88,8 @@ class RechercheController extends Controller // ── Tri + pagination ──────────────────────────────────────────────── $total = $query->count(); $resultats = $query - ->orderByRaw('nom ASC NULLS LAST') - ->orderByRaw('date_evenement ASC NULLS LAST') + ->orderByRaw(DbCompat::nullsLast('nom')) + ->orderByRaw(DbCompat::nullsLast('date_evenement')) ->paginate(25) ->withQueryString(); diff --git a/app/Support/DbCompat.php b/app/Support/DbCompat.php index 180696d..2cdb9fa 100644 --- a/app/Support/DbCompat.php +++ b/app/Support/DbCompat.php @@ -58,6 +58,18 @@ class DbCompat : "JSON_UNQUOTE(JSON_EXTRACT(data, '$.$jsonKey'))"; } + /** + * Fragment ORDER BY "colonne ASC, nulls en dernier". + * Usage : ->orderByRaw(DbCompat::nullsLast('nom')) + */ + public static function nullsLast(string $column, string $direction = 'ASC'): string + { + $dir = strtoupper($direction); + return self::isPgsql() + ? "{$column} {$dir} NULLS LAST" + : "({$column} IS NULL) ASC, {$column} {$dir}"; + } + /** Syntaxe de la colonne générée stockée pour un champ JSON imbriqué (ex: date_evenement.valeur) */ public static function generatedJsonNestedCol(string $jsonPath): string {