Fix compatibilité MySQL : NULLS LAST et order withCount/select
- DbCompat::nullsLast() : syntaxe portable pgsql/mysql pour ORDER BY NULLS LAST - RechercheController, ExportController : remplace 'nom ASC NULLS LAST' (pgsql only) - CarteController : select() avant withCount() pour ne pas effacer le COUNT subquery Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -21,8 +21,8 @@ class CarteController extends Controller
|
|||||||
->whereHas('sources.releves')
|
->whereHas('sources.releves')
|
||||||
->with([
|
->with([
|
||||||
'sources' => function ($q) {
|
'sources' => function ($q) {
|
||||||
$q->withCount('releves')
|
$q->select('id', 'nom', 'lieu_id', 'status', 'annee_debut', 'annee_fin')
|
||||||
->select('id', 'nom', 'lieu_id', 'status', 'annee_debut', 'annee_fin')
|
->withCount('releves')
|
||||||
->orderBy('nom');
|
->orderBy('nom');
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ class ExportController extends Controller
|
|||||||
$query->whereRaw('date_evenement <= ?', [$request->integer('annee_fin') . '-12-31']);
|
$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()) {
|
if ($releves->isEmpty()) {
|
||||||
return back()->with('error', 'Aucun relevé à exporter.');
|
return back()->with('error', 'Aucun relevé à exporter.');
|
||||||
|
|||||||
@@ -88,8 +88,8 @@ class RechercheController extends Controller
|
|||||||
// ── Tri + pagination ────────────────────────────────────────────────
|
// ── Tri + pagination ────────────────────────────────────────────────
|
||||||
$total = $query->count();
|
$total = $query->count();
|
||||||
$resultats = $query
|
$resultats = $query
|
||||||
->orderByRaw('nom ASC NULLS LAST')
|
->orderByRaw(DbCompat::nullsLast('nom'))
|
||||||
->orderByRaw('date_evenement ASC NULLS LAST')
|
->orderByRaw(DbCompat::nullsLast('date_evenement'))
|
||||||
->paginate(25)
|
->paginate(25)
|
||||||
->withQueryString();
|
->withQueryString();
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,18 @@ class DbCompat
|
|||||||
: "JSON_UNQUOTE(JSON_EXTRACT(data, '$.$jsonKey'))";
|
: "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) */
|
/** 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
|
public static function generatedJsonNestedCol(string $jsonPath): string
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user