Laravel Phone

propaganistas/laravel-phone image

Laravel Phone stats

Downloads
15.8M
Stars
2,290
Open Issues
0
Forks
199

View on GitHub →

Adds phone number functionality to Laravel based on Google's libphonenumber API.

Laravel Phone Package Summary

Laravel Phone is a comprehensive Laravel package that enhances applications with sophisticated phone number handling capabilities. Built on the robust libphonenumber by Google, this package simplifies the integration of phone number validation, formatting, and utility functions into your Laravel projects.

Key Features

  • Phone Number Validation: Easily validate phone numbers with options to limit valid numbers to specific countries, number types (like mobile or fixed_line), and lenient validation modes.
  • Attribute Casting: Automatically cast phone number fields in Eloquent models to standardized formats or as raw input, facilitating easier management and storage.
  • Utility Class: Utilize the PhoneNumber utility class for comprehensive manipulation and comparison of phone numbers, including various formatting options and country-specific functionalities.
  • Database Storage and Retrieval: Store phone numbers in your preferred format (E.164, national, etc.) and retrieve or display them as needed, maintaining the integrity and usability of phone data.

Installation and Usage

To install, run:

composer require propaganistas/laravel-phone

Laravel will auto-discover the service provider. Add a validation line in your language files:

'phone' => 'The :attribute field must be a valid number.',

Validation Examples

You can specify validation rules directly in the model or form request:

'phonefield' => 'phone:US,BE',

For dynamic validation based on other input fields, configure as follows:

'phonefield' => 'phone',
'phonefield_country' => 'required_with:phonefield',

Validation can also be extended to include number types and leniency:

'phonefield' => 'phone:mobile,LENIENT',

Attribute Casting

Define how phone numbers should be cast in your Eloquent models:

class User extends Model
{
public $casts = [
'phone_1' => RawPhoneNumberCast::class.':BE',
'phone_2' => E164PhoneNumberCast::class.':BE',
];
}

Utility Usage

The PhoneNumber class allows for extensive manipulation and formatting:

$phone = new PhoneNumber('012 34 56 78', 'BE');
echo $phone->formatInternational(); // +32 12 34 56 78

Database Integration

Consider how phone numbers will be stored, retrieved, and searched in your database. The package supports various strategies for storing numbers in raw, national, or E.164 formats, and provides guidance on setting up searchable phone number fields.

Conclusion

The Laravel Phone package is a powerful tool for developers needing to handle phone number data within their Laravel applications, providing robust validation, formatting, and utility operations that integrate seamlessly with Laravel's Eloquent ORM and validation facilities.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Propaganistas Laravel Phone Related Articles

Build a Laravel Scout Search Endpoint With the HTTP QUERY Method image

Build a Laravel Scout Search Endpoint With the HTTP QUERY Method

Read article
Intercept: Middleware Guardrails for Laravel AI Agents image

Intercept: Middleware Guardrails for Laravel AI Agents

Read article
Laravel WhatsApp: Two Backends Behind One Facade image

Laravel WhatsApp: Two Backends Behind One Facade

Read article
Attach Addresses to Any Eloquent Model with Laravel Addressable image

Attach Addresses to Any Eloquent Model with Laravel Addressable

Read article
Polyscope for Windows is Now Available image

Polyscope for Windows is Now Available

Read article
Mask Sensitive Eloquent Attributes on Retrieval in Laravel image

Mask Sensitive Eloquent Attributes on Retrieval in Laravel

Read article
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup