ellisio / laravel-phone by ellisio

A phone validator for Laravel using the free Twilio phone lookup service.
25,728
12
1
Package Data
Maintainer Username: ellisio
Maintainer Contact: awellis89@gmail.com (Andrew Ellis)
Package Create Date: 2017-01-10
Package Last Update: 2022-04-21
Language: PHP
License: MIT
Last Refreshed: 2024-11-20 03:01:26
Package Statistics
Total Downloads: 25,728
Monthly Downloads: 6
Daily Downloads: 0
Total Stars: 12
Total Watchers: 1
Total Forks: 9
Total Open Issues: 3

Introduction

A phone validator for Laravel 5.5+ using the free Twilio Lookup API.

This package gives developers the ability to validate phone numbers and format phone numbers. All data will be pulled from the Twilio Lookup API.

Validation can be configured to check if the number is valid, or if it is valid within a given list of ISO-3166-1 Alpha 2 country codes.

Installation

Step 1

Install via composer:

composer require ellisio/laravel-phone

Step 2

Add your Twilio credentials to your .env file. If you don't have a Twilio account you can register for one here for free.

TWILIO_ACCOUNT_SID=xxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxx

Step 3 (Optional)

If you want to customize the configuration for this package, you can publish the config file to /config/phone.php by running the following command:

php artisan vendor:publish --provider=EllisIO/Phone/PhoneServiceProvider --tag=config

If you want to customize the output of the validation messages, you can publish the translations file to /lang/vendor/laravel-phone by running the following command:

php artisan vendor:publish --provider=EllisIO/Phone/PhoneServiceProvider --tag=translations

Usage

Basic Validation

If you need to verify that the given number is valid and that is all, use the phone rule.

return [
    'phone' => 'required|phone',
];

Country Validation

If you need to validate that the given number is valid in a list of countries, use the phone_country:US,CA rule. You can list as many ISO-3166-1 Alpha 2 as you wish delimited by a comma.

return [
    'phone' => 'required|phone_country:US,CA',
];

Formatting Phone Numbers

If you want to format the phone number using INTERNATIONAL_FORMAT use the following code:

use EllisIO\Phone\Facades\Phone;

app(Phone::class)->formatNumber('5551234567');

Creating Phone Number Objects

This library includes the ability to generate a Phone object. This object contains the following details about a number:

  • countryCode: ISO-3166 alpha 2 country code.
  • number: E.164 number.
  • formattedNumber: National formatted number.
use EllisIO\Phone\Facades\Phone;

$phone = app(Phone::class)->getPhone('5551234567');
$phone->getNumber(); // Returns "+15551234567"
$phone->getNationalNumber(); // Returns "5551234567"
$phone->getFormattedNumber(); // Returns "(555) 123-4567"
$phone->getCountry(); // Returns "US"
$phone->getCountryCallingCode(); // Returns "1"

Support

Need help? Create an issue.