Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Faker generating strange phone numbers

Generate a unique mobile phone number with Faker in Laravel

Do you want to Generate a unique mobile phone number with Faker in the Laravel application?

Generate a unique mobile phone number laravel

Faker generating strange phone numbers in Laravel

This step-by-step tutorial helps you learn how to create a Factory and insert dummy unique phone number records in the table in the Laravel application with the help of Factory.

To see an example of how to write a factory we will create a users table and create a factory to insert some dummy user records with unique phone numbers in the users table.

Why you should use Faker to generate strange phone numbers

When testing your application or seeding your database, you may need to insert a few records into your database. Instead of manually specifying the value of each column, Laravel allows you to define a set of default attributes for each of your Eloquent models using model factories.

In this guide, I will tell you how to create and use a factory in the Laravel 9 application using Eloquent Factories.

database/migrations/create_users_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone')->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');
    }
};

Next, you can migrate table properties in the database with the artisan migrate command:

php artisan migrate
user table migration in laravel

Defining Model Factories – Generating Factories

php artisan make:factory UserFactory

The new factory class will be placed in your <strong>database/factories</strong> directory.

database\factories\UserFactory.php

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'phone' => fake()->numerify('##########'),
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     *
     * @return static
     */
    public function unverified()
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }
}

##### is commonly used to indicate a phone extension.

Define Factory in Database Seeder Trait

database\seeders\DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\User::factory(10)->create();
    }
}

Run your Seeder File

php artisan db:seed

Output:-

Laravel Faker Phone Number Example

I hope that this article helped you learn How to Generate a unique mobile phone number with faker in Laravel 9. You may also want to check out our guide on How To Create Factory In Laravel 9 example in the Laravel application.

Read also:- Export and Import model as JSON file example in Laravel.