Created
November 22, 2025 10:06
-
-
Save IgorDePaula/c035e4a6058349df555607d24310adf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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