laravel faker duplicate data
up vote
0
down vote
favorite
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 CarbonCarbon;
use FakerGenerator as Faker;
$factory->define(AppHit::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,
];
});
Then in the HitsTableSeeder
it does this:
<?php
use IlluminateDatabaseSeeder;
class HitsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$i = 0;
while ($i <= 25000) {
factory(AppHit::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
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
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?
NOTE: I'm accepting @Erick Patrick's answer because he's correct, but the method I had been using is correct as well. The issue turned out to be that the Hit
model had an event attached to geocode the hit before saving it-- and for some reason the geocoder always returned this same location for the faked data, presumably because it couldn't be found!
php laravel laravel-5 faker
add a comment |
up vote
0
down vote
favorite
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 CarbonCarbon;
use FakerGenerator as Faker;
$factory->define(AppHit::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,
];
});
Then in the HitsTableSeeder
it does this:
<?php
use IlluminateDatabaseSeeder;
class HitsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$i = 0;
while ($i <= 25000) {
factory(AppHit::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
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
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?
NOTE: I'm accepting @Erick Patrick's answer because he's correct, but the method I had been using is correct as well. The issue turned out to be that the Hit
model had an event attached to geocode the hit before saving it-- and for some reason the geocoder always returned this same location for the faked data, presumably because it couldn't be found!
php laravel laravel-5 faker
Where is$faker
defined?
– bishop
Nov 8 at 4:27
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 CarbonCarbon;
use FakerGenerator as Faker;
$factory->define(AppHit::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,
];
});
Then in the HitsTableSeeder
it does this:
<?php
use IlluminateDatabaseSeeder;
class HitsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$i = 0;
while ($i <= 25000) {
factory(AppHit::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
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
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?
NOTE: I'm accepting @Erick Patrick's answer because he's correct, but the method I had been using is correct as well. The issue turned out to be that the Hit
model had an event attached to geocode the hit before saving it-- and for some reason the geocoder always returned this same location for the faked data, presumably because it couldn't be found!
php laravel laravel-5 faker
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 CarbonCarbon;
use FakerGenerator as Faker;
$factory->define(AppHit::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,
];
});
Then in the HitsTableSeeder
it does this:
<?php
use IlluminateDatabaseSeeder;
class HitsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$i = 0;
while ($i <= 25000) {
factory(AppHit::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
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
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?
NOTE: I'm accepting @Erick Patrick's answer because he's correct, but the method I had been using is correct as well. The issue turned out to be that the Hit
model had an event attached to geocode the hit before saving it-- and for some reason the geocoder always returned this same location for the faked data, presumably because it couldn't be found!
php laravel laravel-5 faker
php laravel laravel-5 faker
edited Nov 9 at 0:12
asked Nov 8 at 4:03
user101289
3,94864076
3,94864076
Where is$faker
defined?
– bishop
Nov 8 at 4:27
add a comment |
Where is$faker
defined?
– bishop
Nov 8 at 4:27
Where is
$faker
defined?– bishop
Nov 8 at 4:27
Where is
$faker
defined?– bishop
Nov 8 at 4:27
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Modify the returned array in your factory, like this:
$factory->define(AppHit::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,
];
});
and your HitsTableSeeder
public function run()
{
factory(AppHit::class, 2500)->create();
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Modify the returned array in your factory, like this:
$factory->define(AppHit::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,
];
});
and your HitsTableSeeder
public function run()
{
factory(AppHit::class, 2500)->create();
}
add a comment |
up vote
2
down vote
accepted
Modify the returned array in your factory, like this:
$factory->define(AppHit::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,
];
});
and your HitsTableSeeder
public function run()
{
factory(AppHit::class, 2500)->create();
}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Modify the returned array in your factory, like this:
$factory->define(AppHit::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,
];
});
and your HitsTableSeeder
public function run()
{
factory(AppHit::class, 2500)->create();
}
Modify the returned array in your factory, like this:
$factory->define(AppHit::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,
];
});
and your HitsTableSeeder
public function run()
{
factory(AppHit::class, 2500)->create();
}
edited Nov 8 at 4:42
answered Nov 8 at 4:28
Erick Patrick
30124
30124
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201404%2flaravel-faker-duplicate-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where is
$faker
defined?– bishop
Nov 8 at 4:27