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!










share|improve this question
























  • Where is $faker defined?
    – bishop
    Nov 8 at 4:27















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!










share|improve this question
























  • Where is $faker defined?
    – bishop
    Nov 8 at 4:27













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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();
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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();
    }





    share|improve this answer



























      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();
      }





      share|improve this answer

























        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();
        }





        share|improve this answer














        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();
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 4:42

























        answered Nov 8 at 4:28









        Erick Patrick

        30124




        30124






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini