mercredi 7 novembre 2018

laravel faker duplicate data

I have a Laravel table for hits. I want to generate a lot of test data to test some charts based on the hit location, so I created a factory like this:

<?php

use Carbon\Carbon;
use Faker\Generator as Faker;

$factory->define(App\Hit::class, function (Faker $faker) {
    $date = Carbon::parse($faker->dateTimeBetween("-2 months", "now")->format('Y-m-d'));
    $faker->seed(rand());

    return [
        'listing_key' => '1234',
        'latitude' => '',
        'longitude' => '',
        'country' => 'US',
        'state' => '',
        'city' => '',
        'created_at' => $date,
        'updated_at' => $date,
    ];
});

Then in the HitsTableSeeder it does this:

<?php

use Illuminate\Database\Seeder;

class HitsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $i = 0;

        while ($i <= 25000) {
            factory(App\Hit::class)->create(
                [
                    'latitude' => $faker->latitude,
                    'longitude' => $faker->longitude,
                    'country' => 'US',
                    'state' => $faker->state,
                    'city' => $faker->city,
                ]
            );
            $i++;
        }
    }
}

It always returns the same data:

-[ RECORD 1 ]------+---------------------------
id                 | 1
latitude           | 41.31
longitude          | -72.92
country            | US
state              | CT
-[ RECORD 2 ]------+---------------------------
id                 | 2
latitude           | 41.31
longitude          | -72.92
country            | US
state              | CT
city               | New Haven
-[ RECORD 3 ]------+---------------------------
id                 | 3
latitude           | 41.31
longitude          | -72.92
country            | US
state              | CT
city               | New Haven

This happens regardless of whether I try to override the factory's values in the table seeder or write the data directly in the factory and don't try to override the value in the seeder.

How can I get truly random data?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire