vincej's avatar
Level 15

How to Create Custom Data with Faker

Faker is great and I am using it. However the data you get out is a little limited. What is I want something it does not provide out if the box, like products ? I had a little look at the github docs but creating custom data seemed a little involved, and perhaps not worth doing if all I want is say 20 of each type.

Does anyone have some snappy "easy to follow" work flow for creating custom data ?

Many thanks

0 likes
17 replies
HRcc's avatar

Usually combination of words/sentences and numbers can provide everything I need when it comes to not included stuff. Could you be more specific with what you want to create and maybe add an example?

vincej's avatar
Level 15

sure -

product name / description / sku / category / supplier / location / colour / size

thanks

bashy's avatar

You may not get the exact data type but you can do IDs, names, pargraphs/words/sentences and places. How can you not make up those? :P

Did you take a look at the docs? https://github.com/fzaninotto/Faker

1 like
HRcc's avatar
  • product name -> word
  • description -> sentence/paragraph
  • sku -> randomNumber/numberBetween
  • category -> word (maybe some random pick from a few pre-generated categories)
  • supplier -> company
  • location -> latitude/longitude, address, randomNumber... anything you need here
  • colour -> the same as category (you don't even need faker here to keep it real :) )
  • size -> randomNumber

I don't see any issues here :)

4 likes
vincej's avatar
Level 15

Of course I can make it up, I've always done it that way when I was just using PHPMyAdmin, however, now that I am using migrations, every time I do a refresh I loose all my seed data.

Yes I looked at the docs, and as I said in my first post, it is a bit questionable if it is worth jumping through those hoops for 20 or so entries.

If there is no easy way of doing it, I will just go back to manual.

Don't you ever sleep - it's 11.30pm in London :o)

vincej's avatar
Level 15

I was assuming I has to build a faker provider.

HRcc's avatar

Well, that depends. But it's always been worth it to me. It's incredibly comfortable to have seeders ready (with products, relationships, users) when you need to refresh your DB.

Actually you don't need to do anything like that if you don't want to. Just create your instance $faker = Faker\Factory::create(); and you're good to go.

// Try this article to help you start. Or this one.

vincej's avatar
Level 15

agreed, I would rather have seed data.

Are you referring to this example from the docs:

$generator = \Faker\Factory::create();
$populator = new Faker\ORM\Propel\Populator($generator);
$populator->addEntity('Author', 5);
$populator->addEntity('Book', 10);
$insertedPKs = $populator->execute();

HRcc's avatar
HRcc
Best Answer
Level 22

No, I've never used it with Laravel. But generally you don't need that. Take this as an example:

$faker = Faker\Factory::create();

for ($i = 0; $i < 100; $i++)
{
  $user = User::create([
    'username' => $faker->userName,
    'first_name' => $faker->firstName,
    'last_name' => $faker->lastName,
    'email' => $faker->email,
    'password' => $faker->word
  ]);
}

That's all it really takes to create 100 random users.

vincej's avatar
Level 15

Ok - I get it. I have a bad habit of over thinking things. So to build products I would do something like you suggested earlier:

$faker = Faker\Factory::create();

for ($i = 0; $i < 100; $i++)
{
  $product = product::create([
    'product' => $faker->word,
    'description' => $faker->sentence,
    'product_code' => $faker->number,

etc, etc

  ]);
}

right ??

Sometimes the obvious defeats us. I was expecting to have to build a provider and that looked more of a handful

HRcc's avatar

Yes :) Read through at least one of those articles I linked and you'll have your seeders ready in no time.

vincej's avatar
Level 15

Many Thanks ! crap - it's 1.10 am in Slovakia ...you must be on the coffee ! :o)

cheers from western Canada !

HRcc's avatar

You're welcome and yes, I am sort of a night owl :)

bashy's avatar

Unless you want to have some sample data with actual products, that's fine. You can always add some arrays for an insert if you wanted to keep real data

I prefer coding at night :P no one to disturb me

MThomas's avatar

@vincej you wrote: "however, now that I am using migrations, every time I do a refresh I loose all my seed data."

just to make sure you know that if you register your seeds in the DatabaseSeeder class you can trigger all seeds upon a migration refresh: php artian migrate:refresh --seed?

midou4210's avatar

Hello, I really liked the idea. I want this same code to generate Arabic names. How can this be done? I hope for help from those with experience.

Please or to participate in this conversation.