22/09/2018, 08:29

Vấn đề lưu số điện thoại trong Database

Trước đây mình đã từng đặt một câu hỏi thế này trên Viblo: https://viblo.asia/q/laravel-login-su-dung-so-dien-thoai-khong-can-ma-nuoc-jeZ1eo1YZWz. Ngày đó mình vẫn thắc mắc rằng không biết các hệ thống người ta lưu trữ số điện thoại kiểu gì để có thể vừa đăng nhập được khi nhập mã vùng hoặc không ...

Trước đây mình đã từng đặt một câu hỏi thế này trên Viblo: https://viblo.asia/q/laravel-login-su-dung-so-dien-thoai-khong-can-ma-nuoc-jeZ1eo1YZWz. Ngày đó mình vẫn thắc mắc rằng không biết các hệ thống người ta lưu trữ số điện thoại kiểu gì để có thể vừa đăng nhập được khi nhập mã vùng hoặc không nhập mã vùng. Ví dụ nhập +840981234789 hoặc 0981234789 hoặc 840981234789 thì đều đăng nhập được.Và sau một thời gian tìm hiểu và nghiên cứu mình cũng đã tìm được giải pháp cho vấn đề này và topic này mình sẽ chia sẻ với các bạn. Bắt đầu thôi...

Để lưu trữ số điện thoại trong DB ta cần lưu mã vùng (+84), mã code (VN) và số quốc gia (0981234789). Như vậy ta sẽ tạo ra 1 bảng là phone_numbers để lưu trữ số điện thoại. Bảng này sẽ có các field là region_code, country_code, national_number và 1 field nữa gọi là e164_format là định dạng số điện thoại có đầy đủ mã vùng và số quốc gia ví dụ +84981234789 Để demo cho chức năng này ta tạo các migrations như sau:

users table:

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

phone_numbers table:

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreatePhoneNumbersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('phone_numbers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('region_code');
            $table->string('country_code');
            $table->string('national_number');
            $table->string('e164_format');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('phone_numbers');
    }
}

Tiếp theo để hỗ trợ chúng ta trong việc parsing, format, validate số điện thoại của các quốc gia trên thế giới ta sẽ nhờ đến 1 thư viện của Google đó là https://github.com/googlei18n/libphonenumber. Rất tiếc thư viện này lại không hỗ trợ PHP.             </div>
            
            <div class=

0