Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Last active August 27, 2018 05:13
Show Gist options
  • Save VictorFursa/b7397d91686e5a5388ca7f907cd9b4a2 to your computer and use it in GitHub Desktop.
Save VictorFursa/b7397d91686e5a5388ca7f907cd9b4a2 to your computer and use it in GitHub Desktop.
Laravel Phone Helper
'phone_number' => "required|unique:users,phone_number,$id|min:10|max:20",
protected function prepareForValidation()
{
$this->offsetSet('phone_number', PhoneHelper::normalize($this->phone_number));
}
<?php
namespace App\Modules\Core\Helpers;
use Exception;
use Propaganistas\LaravelPhone\PhoneNumber;
class PhoneHelper
{
/**
* @return string
*/
public static function normalize($phone)
{
$phone = preg_replace('/[^\d\+]/', '', $phone);
try {
return PhoneNumber::make($phone, 'UA')->formatE164();
} catch (Exception $exception) {
return $phone;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment