Files
mesreleves-php/app/Http/Controllers/CarteController.php
T
yann64 bc4dd29ae7 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>
2026-06-05 18:46:31 +02:00

57 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers;
use App\Models\Lieu;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class CarteController extends Controller
{
public function index(): View
{
return view('carte.index');
}
public function data(Request $request): JsonResponse
{
$lieux = Lieu::whereNotNull('latitude')
->whereNotNull('longitude')
->whereHas('sources.releves')
->with([
'sources' => function ($q) {
$q->select('id', 'nom', 'lieu_id', 'status', 'annee_debut', 'annee_fin')
->withCount('releves')
->orderBy('nom');
},
])
->get()
->map(function (Lieu $lieu) {
$relevesTotal = $lieu->sources->sum('releves_count');
return [
'id' => $lieu->id,
'nom' => $lieu->nom_long ?? $lieu->nom,
'lat' => (float) $lieu->latitude,
'lng' => (float) $lieu->longitude,
'sources_count' => $lieu->sources->count(),
'releves_count' => $relevesTotal,
'sources' => $lieu->sources->map(fn ($s) => [
'id' => $s->id,
'nom' => $s->nom,
'releves_count' => $s->releves_count,
'status' => $s->status->label(),
'status_value' => $s->status->value,
'annees' => $s->annee_debut
? ($s->annee_debut === $s->annee_fin || ! $s->annee_fin
? (string) $s->annee_debut
: "{$s->annee_debut}{$s->annee_fin}")
: null,
]),
];
});
return response()->json($lieux);
}
}