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

A Practical Guide to Laravel's First-Party Image Processing image

A Practical Guide to Laravel's First-Party Image Processing

Read article
Laravel SMS Catcher: A Local Dashboard for SMS Notifications image

Laravel SMS Catcher: A Local Dashboard for SMS Notifications

Read article
How to Use the HTTP QUERY Method in Laravel with Scout Search image

How to Use the HTTP QUERY Method in Laravel with Scout Search

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
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
PhpStorm logo

PhpStorm

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

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
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