tapp/filament-google-autocomplete-field
Installation
composer require tapp/filament-google-autocomplete-field
Publish the config file (if needed):
php artisan vendor:publish --provider="Tapp\FilamentGoogleAutocompleteField\FilamentGoogleAutocompleteFieldServiceProvider"
Register the Plugin
Add to app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
\Tapp\FilamentGoogleAutocompleteField\FilamentGoogleAutocompleteFieldPlugin::make(),
]);
}
First Use Case Add to a Filament form/resource:
use Tapp\FilamentGoogleAutocompleteField\Fields\GoogleAutocompleteField;
GoogleAutocompleteField::make('address')
->apiKey(config('services.google.api_key'))
->language('en')
->country('us')
Basic Integration
Use in a Filament Form or Table column:
GoogleAutocompleteField::make('location')
->required()
->rules(['required', 'string'])
->columnSpanFull()
Customizing Fields Extract specific address components:
GoogleAutocompleteField::make('full_address')
->fields([
'street_number',
'route',
'locality',
'administrative_area_level_1',
'country',
'postal_code',
])
Dynamic API Key
Fetch from .env or a config file:
GoogleAutocompleteField::make('address')
->apiKey(env('GOOGLE_MAPS_API_KEY'))
Validation & Storage Validate and store formatted address:
->afterStateUpdated(function (string $state, GoogleAutocompleteField $field) {
$this->saveFormattedAddress($state);
})
Table Column Display Show formatted address in a table:
use Tapp\FilamentGoogleAutocompleteField\Columns\GoogleAutocompleteColumn;
GoogleAutocompleteColumn::make('address')
->formatStateUsing(fn ($state) => $state['formatted_address'])
API Key Management
Store keys in config/services.php or environment variables.
Example:
'google' => [
'api_key' => env('GOOGLE_MAPS_API_KEY'),
],
Caching Responses Cache API responses to reduce calls:
->useCache(true)
->cacheDuration(now()->addHours(1))
Localization Set language/country dynamically:
->language($user->preferredLanguage())
->country($user->countryCode())
Error Handling Handle API errors gracefully:
->onError(function (GoogleAutocompleteField $field, $error) {
$field->addError($error);
})
API Key Restrictions
Rate Limits
Field Validation
->rules(['required', 'array', 'min:1'])
CORS Issues
Deprecated Methods
API Response Logging Enable debug mode to log API responses:
->debug(true)
Network Tab Inspection Use browser dev tools to inspect failed requests (check for CORS or 403 errors).
State Handling The field returns raw Google Places data. Format it before saving:
->formatStateUsing(fn ($state) => $state['formatted_address'] ?? null)
Custom Components Extend the field’s Blade view:
->bladeView('custom.google-autocomplete')
API Response Transformation Modify the response before it’s stored:
->transformResponseUsing(function ($response) {
return collect($response)->only(['formatted_address', 'geometry']);
})
Event Listeners Listen to autocomplete events:
GoogleAutocompleteField::make('address')
->listenForEvents([
'autocomplete.suggestion' => function ($suggestion) {
// Handle suggestion
},
])
Testing Mock API responses in tests:
$this->partialMock(
\SachinAgarwal1337\GooglePlacesApi\GooglePlacesApi::class,
['autocomplete']
);
Batch Processing Use the field in bulk actions (e.g., import tools) with caution—rate limits apply per API key.
Fallback for Offline Provide a fallback input if the API fails:
->fallbackTextInput()
Geocoding Integration
Combine with tapp/filament-google-maps for map visualization:
GoogleMapsField::make('map')
->latitude($address['geometry']['location']['lat'])
->longitude($address['geometry']['location']['lng'])
How can I help you explore Laravel packages today?