How to Generate fake data with Faker package in Laravel

php artisan make:seeder CustomerFakeDataSeeder
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Faker\Factory as Faker;

class CustomerFakeDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $faker = Faker::create();

        foreach (range(1,100) as $index) {
	        DB::table('customers')->insert([
	            'first_name' => $faker->firstName,
	            'last_name' => $faker->lastName,
	            'email' => $faker->email,
	            'mobile' => $faker->phoneNumber,
	        ]);
	    }

    }
}
<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UsersTableSeeder::class);
        $this->call(CustomerFakeDataSeeder::class);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *