Installation
composer require arthem/google-api
Ensure your composer.json includes the package under require.
Configuration
Add your Google API key to .env:
GOOGLE_API_KEY=your_api_key_here
Publish the config file (if available) or manually define it in config/services.php:
'google' => [
'key' => env('GOOGLE_API_KEY'),
],
First Use Case: Places API Fetch nearby places in Laravel:
use Arthem\GoogleApi\Places\PlacesService;
$places = app(PlacesService::class)->nearbySearch('40.7128,-74.0060'); // NYC coordinates
dd($places);
examples/ folder for ready-to-use snippets.Arthem\GoogleApi\Places\PlacesService for Places API methods.Service Layer Integration Inject the service into a Laravel controller or command:
public function __construct(private PlacesService $placesService) {}
Request Handling Use Laravel’s HTTP client to wrap API calls (if the package supports it):
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . config('google.key'),
])->get('https://maps.googleapis.com/...');
Response Processing Normalize responses with Laravel collections or DTOs:
$places = collect($placesService->nearbySearch('lat,lng'))
->map(fn ($place) => new PlaceResource($place));
Caching Responses Cache API responses to reduce calls (e.g., using Laravel’s cache):
return Cache::remember("places_{$lat}_{$lng}", now()->addHours(1), function () use ($placesService, $lat, $lng) {
return $placesService->nearbySearch("{$lat},{$lng}");
});
try-catch blocks to handle GoogleApiException (if defined) or HTTP errors.throttle middleware for API endpoints if needed.$this->mock(PlacesService::class)->shouldReceive('nearbySearch')->andReturn([...]);
API Key Management
.env.Quota Limits
Deprecation Risks
composer.json to avoid breaking updates:
"arthem/google-api": "1.0.0"
Undocumented Features
dd() or dump() to inspect raw API responses:
dd($placesService->nearbySearch('lat,lng')->getRawResponse());
Log::debug('Google Places API call', ['params' => $params, 'response' => $response]);
Custom Endpoints Extend the package by creating a decorator or proxy:
class CustomPlacesService extends PlacesService {
public function customEndpoint($params) {
return $this->client->get('https://custom-endpoint.com', $params);
}
}
Middleware Add middleware to the service’s HTTP client for logging/auth:
$client = Http::withOptions([
'debug' => true,
]);
Configuration Overrides
Override default config in config/google.php:
'places' => [
'base_url' => 'https://custom-mirror.com/places',
],
How can I help you explore Laravel packages today?