Nasim Redoy

Web Developer

Freelancer

Nasim Redoy

Web Developer

Freelancer

Blog Post

Laravel 8 Generate fake data using Faker and Factory

November 1, 2021 Laravel
Laravel 8 Generate fake data using Faker and Factory
663 Views

Using Factory and Faker that ships with Laravel, It’s not that hard to generate thousands of posts in a matter of seconds. With a bit of setup, It can be done. I am going to show you how you can generate fake data using Faker and Factory in Laravel.

Following this step-by-step tutorial is easy. There is also an easier way of doing it shown at the end of this article.

Requirements:

  • Make sure you have a fresh install of laravel
  • Create a database and inform .env

Create a Address model

php artisan make:model Address -m​

Add fields to the Address migration

 Schema::create('addresses', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('mobile');
            $table->integer('nid');
            $table->string('address');
            $table->timestamps();
        });

Run the migration

php artisan migrate​

Create Address Factory

php artisan make:factory AddressFactory --model="Address"

Go to database/factories/AddressFactory.php. There you will see AddressFactory already Created.

Put the following code right below. This is for the Address model, You can modify it for other models adding more fields as needed.
Here we are using faker to create name,mobile , nid and address fields

protected $model = Address::class;
    public function definition()
    {
        return [
            'name' => $this -> faker->name(),
            'mobile' => $this -> faker->biasedNumberBetween(1,100000),
            'nid' => $this -> faker->randomDigit(),
            'address' => $this -> faker->address(),
        ];
    }

To test this, open up tinker:

php artisan tinker

Run the factory command:

Address::factory()->make();

You should get a response similar to this:

=> App\Models\Address {#3583
     name: "Rossie Williamson",
     mobile: 39826,
     nid: 5,
     address: """
       40790 Amber Flats Apt. 223\n
       Eulastad, CO 91888
       """,
   }

This creates the Address model; it doesn’t actually save it to the database. To save it to the database, you can use create() instead of make().

Now we try to create 10 Address.Laravel makes this painless as well. Let’s look at it in tinker again.

Address::factory()->count(10)->create();

php artisan tinker



//Run This Command

>>> Address::factory()->count(10)->create();



[!] Aliasing 'Address' to 'App\Models\Address' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#3581
     all: [
       App\Models\Address {#3585
         name: "Lou Torphy",
         mobile: 14912,
         nid: 0,
         address: """
           9389 Walker Port\n
           Deckowborough, NM 21134-3213
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 1,
       },
       App\Models\Address {#3586
         name: "Prof. Kasey Kling IV",
         mobile: 59004,
         nid: 8,
         address: """
           1457 Sammie Ports Suite 437\n
           Mayertfurt, AL 42734
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 2,
       },
       App\Models\Address {#3587
         name: "Oscar Heidenreich IV",
         mobile: 66000,
         nid: 7,
         address: """
           1780 Auer Ridges Suite 719\n
           Annettabury, CT 93282-1489
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 3,
       },
       App\Models\Address {#3588
         name: "Tiara Kessler",
         mobile: 81388,
         nid: 4,
         address: """
           498 Zboncak Ferry Suite 236\n
           Lake Leila, WV 49784
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 4,
       },
       App\Models\Address {#3589
         name: "Vena Medhurst MD",
         mobile: 19324,
         nid: 8,
         address: """
           3924 Hand Path Apt. 410\n
           East Cecil, OH 72056
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 5,
       },
       App\Models\Address {#3590
         name: "Marlene Graham",
         mobile: 13566,
         nid: 7,
         address: """
           705 Jaquan Stravenue Suite 359\n
           New Tara, GA 63136-0829
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 6,
       },
       App\Models\Address {#3591
         name: "Miss Kenyatta Runolfsson MD",
         mobile: 86880,
         nid: 1,
         address: """
           779 Candido Row Apt. 710\n
           New Jairoside, MT 07970-4423
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 7,
       },
       App\Models\Address {#3592
         name: "Lauretta Hayes",
         mobile: 63779,
         nid: 7,
         address: """
           4735 Braden Well\n
           Port Leannehaven, DC 13928
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 8,
       },
       App\Models\Address {#3593
         name: "Gabe Luettgen",
         mobile: 23231,
         nid: 5,
         address: """
           4420 Weldon Squares Suite 819\n
           Coryport, OH 26322
           """,
         updated_at: "2021-11-01 18:40:39",
         created_at: "2021-11-01 18:40:39",
         id: 9,
       },
       App\Models\Address {#3594
         name: "Dr. Torey Larkin",
         mobile: 71971,
         nid: 8,
         address: """
           1128 Ziemann Tunnel\n
           Spinkamouth, MD 07035
           """,
         updated_at: "2021-11-01 18:40:40",
         created_at: "2021-11-01 18:40:40",
         id: 10,
       },
     ],
   }
>>> O2A

Recent Article

Laravel 8 Generate fake data using Faker and Factory
November 1, 2021 Laravel 8 Generate fake data using Faker and Factory
Laravel

663 ViewsUsing Factory and Faker that ships with Laravel, It’s not that hard to generate thousands of posts in a matter of seconds. With…

laravel resource route controller crud
August 17, 2021 Laravel 8 Resource Controller & Routing Tutorial
Laravel

884 Views     Today,I will explain you how to create resource route in laravel 8. we will show laravel 8…

Taggs:
Write a comment