Laravel Fakerr重复数据

时间:2018-11-08 04:03:54

标签: php laravel laravel-5 faker

我有一个$ ./bin/select_peekstdin < file doing input routine $ ./bin/select_peekstdin doing output routine 的Laravel表。我想根据点击位置生成大量测试数据以测试某些图表,所以我创建了一个像这样的工厂:

hits

然后在<?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 [ 'latitude' => '', 'longitude' => '', 'country' => 'US', 'state' => '', 'city' => '', 'created_at' => $date, 'updated_at' => $date, ]; }); 中执行以下操作:

HitsTableSeeder

它总是返回相同的数据:

<?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++;
        }
    }
}

无论我是尝试覆盖表播种器中的工厂值还是直接在工厂中写入数据,并且不要尝试覆盖播种器中的值,都会发生这种情况。

如何获取真正的随机数据?

注意:我接受@Erick Patrick的回答,因为他是正确的,但是我一直使用的方法也是正确的。原来的问题是,-[ RECORD 1 ]------+--------------------------- id | 1 latitude | 41.31 longitude | -72.92 country | US state | CT city | New Haven -[ 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 模型附加了一个event,以便在保存匹配之前对匹配进行地理编码-出于某种原因,地理编码器始终为伪造数据返回相同的位置,大概是因为找不到!

1 个答案:

答案 0 :(得分:2)

修改工厂中返回的数组,如下所示:

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

    return [
        'latitude' => $faker->latitude($min = -90, $max = 90),   
        'longitude' => $faker->longitude($min = -180, $max = 180),
        'country' => $faker->countryCode,
        'state' => $faker->stateAbbr,
        'city' => $faker->country,
        'created_at' => $date,
        'updated_at' => $date,
    ];
});

和您的HitsTableSeeder

public function run()
{
    factory(App\Hit::class, 2500)->create();
}