Download the PHP package propaganistas/laravel-phone without Composer

On this page you can find all versions of the php package propaganistas/laravel-phone. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-phone

Laravel Phone

Tests Latest Stable Version Total Downloads License

Adds phone number functionality to Laravel based on the PHP port of libphonenumber by Google.

Table of Contents

Demo

Check out the behavior of this package in the demo.

Installation

Run the following command to install the latest applicable version of the package:

The Service Provider gets discovered automatically by Laravel.

In your languages directory, add an extra translation in every validation.php language file:

Validation

Use the phone keyword in your validation rules array or use the Propaganistas\LaravelPhone\Rules\Phone rule class to define the rule in an expressive way.

To put constraints on the allowed originating countries, you can explicitly specify the allowed country codes.

Or to make things more dynamic, you can also match against another data field holding a country code. For example, to require a phone number to match the provided country of residence. Make sure the country field has the same name as the phone field but with _country appended for automatic discovery, or provide your custom country field name as a parameter to the validator:

Note: country codes should be ISO 3166-1 alpha-2 compliant.

To support any valid internationally formatted phone number next to the whitelisted countries, use the INTERNATIONAL parameter. This can be useful when you're expecting locally formatted numbers from a specific country but also want to accept any other foreign number entered properly:

To specify constraints on the number type, just append the allowed types to the end of the parameters, e.g.:

The most common types are mobile and fixed_line, but feel free to use any of the types defined here.

Prepend a type with an exclamation mark to blacklist it instead. Note that you can never use whitelisted and blacklisted types at the same time.

You can also enable lenient validation by using the LENIENT parameter. With leniency enabled, only the length of the number is checked instead of actual carrier patterns.

Attribute casting

Two cast classes are provided for automatic casting of Eloquent model attributes:

Both classes automatically cast the database value to a PhoneNumber object for further use in your application.

When setting a value, they both accept a string value or a PhoneNumber object. The RawPhoneNumberCast mutates the database value to the raw input number, while the E164PhoneNumberCast writes a formatted E.164 phone number to the database.

In case of RawPhoneNumberCast, the cast needs to be hinted about the phone country in order to properly parse the raw number into a phone object. In case of E164PhoneNumberCast and the value to be set is not already in some international format, the cast needs to be hinted about the phone country in order to properly mutate the value.

Both classes accept cast parameters in the same way:

  1. When a similar named attribute exists, but suffixed with _country (e.g. phone_country), the cast will detect and use it automatically.
  2. Provide another attribute's name as a cast parameter
  3. Provide one or several country codes as cast parameters

Important note: Both casts expect valid phone numbers in order to smoothly convert from/to PhoneNumber objects. Please validate phone numbers before setting them on a model. Refer to the validation documentation to learn how to validate phone numbers.

⚠️ Attribute assignment and E164PhoneNumberCast

Due to the nature of E164PhoneNumberCast a valid country attribute is expected if the number is not passed in international format. Since casts are applied in the order of the given values, be sure to set the country attribute before setting the phone number attribute. Otherwise E164PhoneNumberCast will encounter an empty country value and throw an unexpected exception.

Utility PhoneNumber class

A phone number can be wrapped in the Propaganistas\LaravelPhone\PhoneNumber class to enhance it with useful utility methods. It's safe to directly reference these objects in views or when saving to the database as they will degrade gracefully to the E.164 format.

Formatting

A PhoneNumber can be formatted in various ways:

Number information

Get some information about the phone number:

Equality comparison

Check if a given phone number is (not) equal to another one:

Helper function

The package exposes the phone() helper function that returns a Propaganistas\LaravelPhone\PhoneNumber instance or the formatted string if $format was provided:

Database considerations

Disclaimer: Phone number handling is quite different in each application. The topics mentioned below are therefore meant as a set of thought starters; support will not be provided.

Storing phone numbers in a database has always been a speculative topic and there's simply no silver bullet. It all depends on your application's requirements. Here are some things to take into account, along with an implementation suggestion. Your ideal database setup will probably be a combination of some of the pointers detailed below.

Uniqueness

The E.164 format globally and uniquely identifies a phone number across the world. It also inherently implies a specific country and can be supplied as-is to the phone() helper.

You'll need:

Example:

Presenting the phone number the way it was inputted

If you store formatted phone numbers the raw user input will unretrievably get lost. It may be beneficial to present your users with their very own inputted phone number, for example in terms of improved user experience.

You'll need:

Example:

Supporting searches

Searching through phone numbers can quickly become ridiculously complex and will always require deep understanding of the context and extent of your application. Here's a possible approach covering quite a lot of "natural" use cases.

You'll need:

Example:


All versions of laravel-phone with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/contracts Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
illuminate/validation Version ^10.0|^11.0
giggsey/libphonenumber-for-php-lite Version ^8.13.35
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package propaganistas/laravel-phone contains the following files

Loading the files please wait ....