If you will only be dealing with US phone numbers, here's how I do it. I use a regular varchar column (why text?) on the table.
On the model I use a setter to format the phone number (remove all but ten digits) prior to storing it and a getter to return it in this format: 444-444-4444:
public function setPhoneMobileAttribute($phone)
{
// Strip all but numbers
$this->attributes['phone_mobile'] = trim(preg_replace('/^1|\D/', "", $phone));
}
public function getPhoneMobileAttribute($phoneNumber)
{
if ($phoneNumber) {
$output = substr($phoneNumber, 0, 3) . '-' . substr($phoneNumber, 3, 3) . '-' . substr($phoneNumber, 6);
return $output;
}
}
On the validation side, I use the following regex. It accepts the phone number in all of the common formats for US numbers, provided that it is a standard ten digit number. So the user can enter it however they wish and at the end it'll be stored as just numbers on the table.
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$phoneRegex = "/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})?$/";
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => ['required', 'email', 'unique:users'],
'phone_cell' => ['required', "regex:$phoneRegex", 'unique:users'],
'user_type_id' => 'required',
'password' => ['required', 'confirmed'],
'username' => ['required', 'unique:users']
];
}