Polyscope - The agent-first dev environment for Laravel

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

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
NativePHP for Mobile Is Now Free image

NativePHP for Mobile Is Now Free

Read article
Meet LaraUtilX: A Utility Toolkit Every Laravel Developer Needs image

Meet LaraUtilX: A Utility Toolkit Every Laravel Developer Needs

Read article
GeoGenius Package for Laravel image

GeoGenius Package for Laravel

Read article
Filament Is Now Running Natively on Mobile image

Filament Is Now Running Natively on Mobile

Read article
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu
Lucky Media logo

Lucky Media

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

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

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell