Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created November 22, 2025 10:06
Show Gist options
  • Select an option

  • Save IgorDePaula/c035e4a6058349df555607d24310adf9 to your computer and use it in GitHub Desktop.

Select an option

Save IgorDePaula/c035e4a6058349df555607d24310adf9 to your computer and use it in GitHub Desktop.
<?php
Forms\Components\TextInput::make('zipcode')->label('CEP')->mask('99999-999')->suffixAction(fn($state, Forms\Set $set) => Forms\Components\Actions\Action::make('search-action')
->icon('heroicon-o-magnifying-glass')
->action(function () use ($state, $set) {
$cepData = static::getCountryData($state);
$set('address', $cepData['logradouro'] ?? null);
$set('neighborhood', $cepData['bairro'] ?? null);
$set('complement', $cepData['complemento'] ?? null);
$set('city', $cepData['localidade'] ?? null);
$set('state', $cepData['uf'] ?? null);
})
)->columnSpanFull()->live(onBlur: true),
//no mesmo recurso, como um metodo a parte
public static function getCountryData(?string $searchTerm = null): ?array
{
if (blank($searchTerm)) {
return null;
}
$searchData = str_replace('-', '', $searchTerm);
try {
$cepData = Http::baseUrl("https://viacep.com.br/ws/{$searchData}/json/")
->get("/")
->throw()
->json();
if (isset($cepData['erro'])) {
Notification::make()->danger()->body('CEP não encontrado')->send();
}
return $cepData;
} catch (RequestException $e) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment