Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Phone Laravel Package

propaganistas/laravel-phone

Add robust phone number validation, casting, and formatting to Laravel using Google’s libphonenumber (PHP port). Validate by country or dynamic country fields, cast model attributes to phone objects, format numbers consistently, and compare/evaluate phone metadata.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:
    • Leverages Google’s libphonenumber: Industry-standard phone validation/formatting, reducing custom logic risk.
    • Seamless Laravel integration: Built for Laravel’s ecosystem (validation, Eloquent casts, helpers).
    • Modular design: Validation, casting, and utility layers are decoupled, enabling targeted adoption.
    • Future-proof: Actively maintained (last release 2026) with Laravel 13 support.
  • Cons:
    • Database schema dependency: Requires careful design (e.g., E.164 vs. raw storage trade-offs).
    • Validation granularity: Country/type constraints add complexity to rules (e.g., phone:US,BE,mobile).

Integration Feasibility

  • High: Zero-config setup (auto-discovery), minimal boilerplate for basic use.
  • Validation: Drop-in replacement for Laravel’s required|string rules (e.g., phone:US).
  • Casting: Eloquent models gain phone-aware attributes with minimal setup (RawPhoneNumberCast/E164PhoneNumberCast).
  • Utility Methods: PhoneNumber class provides formatting/comparison without external dependencies.

Technical Risk

  • Low-Medium:
    • Dependency Risk: Underlying libphonenumber-for-php-lite is stable but not Laravel-specific.
    • Schema Risk: Poor database design (e.g., not storing raw input) may limit UX (e.g., user-editable phone numbers).
    • Performance: Heavy validation (e.g., LENIENT mode) could impact bulk operations.
  • Mitigations:
    • Test edge cases (e.g., invalid country codes, malformed inputs).
    • Benchmark validation overhead in high-throughput flows.

Key Questions

  1. Schema Strategy:
    • Should we store raw input + country (for UX) or E.164 (for uniqueness)?
    • Do we need searchable variants (e.g., normalized/national/E.164)?
  2. Validation Scope:
    • Should all phone fields use strict validation (e.g., phone:US,mobile) or lenient (LENIENT)?
  3. Internationalization:
    • Will we support dynamic country detection (e.g., via user profiles) or static rules?
  4. Error Handling:
    • How should invalid numbers be logged/handled (e.g., retries, user notifications)?
  5. Testing:
    • Are there regional phone number edge cases (e.g., toll-free, VoIP) to validate?

Integration Approach

Stack Fit

  • Laravel Core: Native support for validation, Eloquent, and Blade helpers.
  • Validation Layer:
    • Replace string rules with phone:US,BE in Form Requests/API resources.
    • Example:
      public function rules() {
          return [
              'contact_number' => 'required|phone:INTERNATIONAL,US,CA',
              'user_phone' => ['phone:BE,mobile', 'required_with:user_country'],
              'user_country' => 'required_with:user_phone|alpha:2',
          ];
      }
      
  • Database Layer:
    • Option 1 (Simplicity): Single phone_e164 column (uniqueness, but loses raw input).
    • Option 2 (Flexibility): phone_raw, phone_country, and derived columns (e.g., phone_normalized).
    • Observer Pattern: Use saving() to precompute searchable variants if needed.
  • Utility Layer:
    • Replace string manipulation with PhoneNumber methods (e.g., $phone->formatNational() in views).
    • Example Blade:
      <a href="tel:{{ $user->phone->formatForMobileDialingInCountry('US') }}">
          {{ $user->phone->formatNational() }}
      </a>
      

Migration Path

  1. Phase 1: Validation-Only
    • Add package, update validation rules (low risk, reversible).
    • Test with existing phone fields (e.g., phone:INTERNATIONAL for global support).
  2. Phase 2: Eloquent Casting
    • Migrate models to use E164PhoneNumberCast (or RawPhoneNumberCast if raw input is critical).
    • Update database schema (e.g., add phone_country column if needed).
  3. Phase 3: Utility Adoption
    • Replace hardcoded phone formatting (e.g., str_replace$phone->formatE164()).
    • Add PhoneNumber to DTOs/services for type safety.

Compatibility

  • Laravel 10+: Fully supported (tested up to Laravel 13).
  • PHP 8.1+: Required for enums in libphonenumber-for-php-lite.
  • Dependencies:
    • No conflicts with common packages (e.g., Laravel Breeze, Sanctum).
    • Potential overlap with giggsey/libphonenumber-for-php (this package is a wrapper).

Sequencing

Priority Task Dependencies
High Add package + validation rules None
High Update model casts Database schema changes
Medium Migrate existing phone data Schema updates
Low Add searchable variants Observer setup
Low Replace string formatting in views Utility layer adoption

Operational Impact

Maintenance

  • Pros:
    • Reduced Custom Logic: No need to maintain phone parsing/validation.
    • Centralized Updates: Package updates include libphonenumber fixes (e.g., new country codes).
  • Cons:
    • Dependency Bloat: Adds ~1MB to vendor size (minimal impact).
    • Validation Rules: Complex rules (e.g., phone:US,BE,mobile) may need documentation.
  • Tooling:
    • Add phpunit tests for phone validation (e.g., test/Feature/PhoneValidationTest.php).
    • Document edge cases (e.g., "How to handle VoIP numbers?").

Support

  • Common Issues:
    • User Input Errors: Invalid formats (e.g., +1 (123) 456-7890 vs. 1234567890).
    • Country Mismatches: Phone numbers not matching expected regions.
    • Database Errors: Schema mismatches (e.g., E164PhoneNumberCast without country).
  • Mitigations:
    • User Feedback: Show formatted numbers back to users (e.g., "Your number: +1 123 456 7890").
    • Logging: Log validation failures with metadata (e.g., phone:invalid_country).
    • Fallbacks: Graceful degradation (e.g., store raw input if parsing fails).

Scaling

  • Performance:
    • Validation: ~1–5ms per number (benchmark with INTERNATIONAL mode).
    • Casting: Negligible overhead (cached PhoneNumber objects).
    • Database: Index phone_e164 for uniqueness; avoid full-text search on phone fields.
  • High-Volume Scenarios:
    • Bulk Imports: Disable validation or use LENIENT mode for initial loads.
    • API Rate Limits: Cache PhoneNumber objects in services (e.g., PhoneNumberService).

Failure Modes

Scenario Impact Mitigation
Invalid Country Code Validation fails silently Add fallback (e.g., phone:INTERNATIONAL)
Malformed Input Database errors (e.g., E164PhoneNumberCast) Pre-validate or use try-catch
Schema Mismatch Casts fail on load Migrate data incrementally
Package Update Breaks Enum changes in libphonenumber Test against dev-main branch
Search Performance Slow queries on phone fields Use derived columns (e.g., phone_normalized)

Ramp-Up

  • Onboarding:
    • Developers:
      • 1-hour workshop on validation rules/casts.
      • Cheat sheet for common formats (e.g., formatNational(), formatForMobileDialingInCountry()).
    • QA:
      • Test plan for edge cases (e.g., empty strings, non-numeric chars).
    • DevOps:
      • Monitor validation failures in logs (e.g., monolog channel).
  • Training:
    • Frontend: How to display phone numbers (e.g., tel: links, international formats).
    • Backend: When to use RawPhoneNumberCast vs. E164PhoneNumberCast.
  • Documentation:
    • Add to internal wiki:
      • Schema examples (e.g., "How to store phone numbers for [use case X]").
      • Validation rule reference (e.g.,
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata