Installation:
composer require zemlecht/laravel-dadata
For Laravel <5.5, manually register MoveMoveIo\DaData\DaDataServiceProvider in config/app.php.
Publish Config:
php artisan vendor:publish --provider="MoveMoveIo\DaData\DaDataServiceProvider"
This generates .env keys (DADATA_TOKEN, DADATA_SECRET) and config/dadata.php.
Configure .env:
DADATA_TOKEN=your_api_token
DADATA_SECRET=your_api_secret
First Use Case: Validate and suggest corrections for a phone number:
use MoveMoveIo\DaData\Facades\DaData;
$phone = DaData::phone()->validate('+7(903)123-45-67');
// Returns corrected phone in E.164 format: "+79031234567"
Validation & Correction: Use facades for common fields (phone, address, name):
// Phone
$corrected = DaData::phone()->suggest('+7(903)123-45-67');
// Address
$suggestions = DaData::address()->suggest('ул. Ленина, д. 10');
// Name
$normalized = DaData::name()->clean('Иван Иванов');
Batch Processing: Process multiple inputs efficiently:
$phones = ['+7(903)123-45-67', '+7(916)789-01-23'];
$results = DaData::phone()->batch($phones);
Integration with Forms: Use middleware to auto-correct form submissions:
// In FormRequest
public function passedValidation()
{
$this->merge([
'phone' => DaData::phone()->validate($this->phone),
]);
}
Caching Responses: Cache frequent queries (e.g., address suggestions) to reduce API calls:
$suggestions = Cache::remember("dadata_address_{$query}", now()->addHours(1), function() use ($query) {
return DaData::address()->suggest($query);
});
Laravel Validation: Extend validation rules:
use MoveMoveIo\DaData\Rules\ValidPhone;
$request->validate([
'phone' => ['required', new ValidPhone],
]);
API Rate Limiting:
Monitor API usage via config/dadata.php:
'rate_limit' => 100, // Max requests per minute
Error Handling: Wrap API calls in try-catch:
try {
$result = DaData::phone()->validate($input);
} catch (\MoveMoveIo\DaData\Exceptions\DaDataException $e) {
report($e);
return back()->withError('DaData service unavailable.');
}
Testing: Use mocks in PHPUnit:
$this->mock(DaData::class)->shouldReceive('phone')->andReturnSelf()
->shouldReceive('validate')->andReturn('+79031234567');
Token/Secret Mismatch:
DADATA_TOKEN and DADATA_SECRET match your DaData account.config/dadata.php for typos in keys.Rate Limits:
429 Too Many Requests.Character Encoding:
$cleanInput = mb_convert_encoding($input, 'UTF-8');
Phone Format Quirks:
8(903)123-45-67 (missing + or 7) may not validate.+7 or use DaData::phone()->clean() first.Address Suggestions:
ул. Л) return fewer results. Use full addresses for accuracy.Enable Logging:
Set debug: true in config/dadata.php to log raw API responses:
'debug' => env('DADATA_DEBUG', false),
API Response Inspection:
Use ->raw() to inspect unprocessed responses:
$raw = DaData::phone()->raw()->validate($phone);
Common HTTP Errors:
Custom Endpoints:
Extend the SDK for unsupported fields (e.g., email):
// In app/Providers/DaDataServiceProvider.php
$this->app->extend('dadata', function ($dadata) {
$dadata->email = new EmailService($dadata->client);
return $dadata;
});
Webhook Integration: Use DaData’s webhook events to sync corrections in real-time:
Route::post('/dadata-webhook', function (Request $request) {
// Process webhook payload
});
Local Testing: Mock the DaData client for CI/CD:
// In tests/CreatesApplication.php
$this->app->instance(\MoveMoveIo\DaData\DaDataClient::class, new MockClient());
Queue Delayed Requests: Offload heavy corrections to queues:
DaData::phone()->later()->validate($phone); // Uses queue
How can I help you explore Laravel packages today?