Publier une annonce
@php
// $data vient du contrôleur: Session::get('annonce_data', [])
$persist = $data ?? [];
// Tags: en session c'est une chaîne "a, b, c" → on retransforme en tableau
$persistTags = [];
if (!empty($persist['tags'])) {
$persistTags = array_values(array_filter(array_map('trim', explode(',', $persist['tags']))));
}
// On unifie la source des tags pour le Blade plus bas
$oldTags = old('tags', $persistTags);
$persistCategoryId = old('category_id', $persist['category_id'] ?? null);
$persistCategoryName = null;
$locale = app()->getLocale();
if ($persistCategoryId) {
try {
$rawName = optional(\App\Models\Category::find($persistCategoryId))->name;
if (is_array($rawName)) {
// name est déjà un tableau ["fr"=>...,"en"=>...]
$persistCategoryName = $rawName[$locale] ?? $rawName['fr'] ?? $rawName['en'] ?? reset($rawName);
} else {
// name est une string (souvent JSON) -> on essaie de décoder
$arr = json_decode($rawName, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($arr)) {
$persistCategoryName = $arr[$locale] ?? $arr['fr'] ?? $arr['en'] ?? reset($arr);
} else {
// string simple
$persistCategoryName = (string) $rawName;
}
}
} catch (\Throwable $e) {
}
}
// Pays
$persistCountryCode = old('country_code', $persist['country_code'] ?? ($country['code'] ?? 'ci'));
$persistCountryName = null;
try {
$rawName = optional(\App\Models\Country::where('code', $persistCountryCode)->first())->name;
if ($rawName) {
$decoded = json_decode($rawName, true);
$persistCountryName = is_array($decoded)
? ($decoded['fr'] ?? $decoded['en'] ?? reset($decoded))
: $rawName;
}
} catch (\Throwable $e) {
}
if (!$persistCountryName) {
$persistCountryName = $country['name'] ?? '';
}
// Ville (selon ton code, parfois c’est un id brut, parfois un JSON)
$oldCityRaw = old('city_id', $persist['city_id'] ?? '');
$persistCityId = $oldCityRaw;
$persistCityName = '';
if (is_string($oldCityRaw) && str_starts_with($oldCityRaw, '{')) {
// ancien format JSON {"id":..,"name":{"fr":".."}}
$json = json_decode($oldCityRaw, true);
if (json_last_error() === JSON_ERROR_NONE) {
$persistCityId = $json['id'] ?? '';
$persistCityName = $json['name']['fr'] ?? ($json['name']['en'] ?? '');
}
} else {
// format id numérique → on tente de retrouver le nom dans la liste du pays chargée côté serveur
$citiesList = collect($country['cities'] ?? []);
$city = $citiesList->firstWhere('id', $persistCityId);
if ($city) {
$persistCityName = $city['name'];
}
}
@endphp