Laravel Integration
Use Postal Code with Laravel applications.
Installation
Section titled “Installation”composer require cline/postal-codeThe service provider is auto-discovered in Laravel.
Validation Rules
Section titled “Validation Rules”Basic Rule
Section titled “Basic Rule”use Cline\PostalCode\Rules\PostalCodeRule;
// In form requestpublic function rules(): array{ return [ 'postal_code' => ['required', new PostalCodeRule('US')], ];}Dynamic Country
Section titled “Dynamic Country”public function rules(): array{ return [ 'country' => ['required', 'string', 'size:2'], 'postal_code' => [ 'required', new PostalCodeRule($this->input('country')), ], ];}Custom Messages
Section titled “Custom Messages”public function rules(): array{ return [ 'postal_code' => ['required', new PostalCodeRule('US')], ];}
public function messages(): array{ return [ 'postal_code' => 'Please enter a valid ZIP code.', ];}Validation Rule String
Section titled “Validation Rule String”// Using rule string (if registered)public function rules(): array{ return [ 'postal_code' => 'required|postal_code:US', ];}
// Register in AppServiceProvideruse Cline\PostalCode\PostalCode;use Illuminate\Support\Facades\Validator;
public function boot(): void{ Validator::extend('postal_code', function ($attribute, $value, $parameters) { $country = $parameters[0] ?? 'US'; return PostalCode::isValid($value, $country); });}Facade
Section titled “Facade”use Cline\PostalCode\Facades\PostalCode;
// Validate$result = PostalCode::validate('90210', 'US');
// Quick checkPostalCode::isValid('90210', 'US');
// FormatPostalCode::format('sw1a1aa', 'GB'); // "SW1A 1AA"Model Casting
Section titled “Model Casting”use Cline\PostalCode\Casts\PostalCodeCast;
class Address extends Model{ protected $casts = [ 'postal_code' => PostalCodeCast::class . ':US', ];}
// Usage$address = new Address();$address->postal_code = '90210';$address->postal_code; // Returns formatted postal codeEloquent Accessor
Section titled “Eloquent Accessor”use Cline\PostalCode\PostalCode;
class Address extends Model{ public function getFormattedPostalCodeAttribute(): string { $result = PostalCode::validate( $this->postal_code, $this->country_code );
return $result->isValid() ? $result->formatted() : $this->postal_code; }}Blade Directive
Section titled “Blade Directive”// Register in AppServiceProviderBlade::directive('postalcode', function ($expression) { return "<?php echo \Cline\PostalCode\PostalCode::format($expression); ?>";});
// Usage in Blade@postalcode($address->postal_code, $address->country)API Resource
Section titled “API Resource”use Cline\PostalCode\PostalCode;use Illuminate\Http\Resources\Json\JsonResource;
class AddressResource extends JsonResource{ public function toArray($request): array { $result = PostalCode::validate( $this->postal_code, $this->country_code );
return [ 'street' => $this->street, 'city' => $this->city, 'postal_code' => $result->formatted(), 'postal_code_valid' => $result->isValid(), 'country' => $this->country_code, ]; }}Configuration
Section titled “Configuration”return [ // Default country for validation 'default_country' => env('POSTAL_CODE_COUNTRY', 'US'),
// Custom handlers 'handlers' => [ // 'XX' => App\PostalCode\CustomHandler::class, ],];php artisan vendor:publish --tag=postal-code-config