Try this
if ($data && array_key_exists('uuid', $data)) { $gpsSalePoint = GpsSalePoint::with('district')->where('uuid', $data['uuid'])->first(); $countryIds = Country::query() ->whereRaw('lower(name) like ?', [Str::of($data['pais'])->squish()->lower()->value()]) ->pluck('id');
if (!$countryIds->contains(tenant('country_id'))) {
// Handle country mismatch
}
$provinceIds = Province::query()
->whereRaw('lower(name) like ?', [Str::of($data['provincia'])->squish()->lower()->value()])
->where('country_id', tenant('country_id'))
->pluck('id');
if ($provinceIds->count() === 0) {
// Handle province mismatch
}
$cantonIds = Canton::query()
->whereRaw('lower(name) like ?', [Str::of($data['canton'])->squish()->lower()->value()])
->whereIn('province_id', $provinceIds)
->pluck('id');
if ($cantonIds->count() === 0) {
// Handle canton mismatch
}
$districtIds = District::query()
->whereRaw('lower(name) like ?', [Str::of($data['distrito'])->squish()->lower()->value()])
->whereIn('canton_id', $cantonIds)
->pluck('id');
if ($districtIds->count() === 0) {
// Handle district mismatch
} else {
$district = District::find($districtIds->first());
if ($district && $district->canton_id == 22) {
// Process the correct district
} else {
// Handle incorrect district or canton_id
}
}
}