[Laravel] create dummy data

1、Suggested that install Generators first.

2、create a table called lessons

$ php artisan generate:migration create_lessons_table --fields="title:string, body:text"
$ php artisan migrate

3、create a seed table file “LessonsTableSeeder.php” in app/database/seeds directory, the code:

<?php

use Faker\Factory as Faker;

class LessonsTableSeeder extends Seeder {

  
  public function run()
  {
    $faker = Faker::create();
    foreach( range(1, 10) as $index )
    {
      Lesson::create([
        
      ]);
    }
  }

}

4、search which library can create dummy data and install it:

$ composer search faker
fzaninotto/faker Faker is a PHP library that generates fake data for you.

$ composer require fzaninotto/faker --dev
Please provide a version constraint for the fzaninotto/faker requirement: 1.3
// then will update composer.json to
"require-dev": {
  "fzaninotto/faker": "1.3"
}

5、update app/database/seeds/LessonsTableSeeder.php

public function run()
{
  $faker = Faker::create();
  foreach( range(1, 30) as $index )
  {
    Lesson::create([
      'title' => $faker->sentence(5),
      'body' => $faker->paragraph(4)
    ]);
  }
}

6、update app/database/seeds/DatabaseSeeder.php

public function run()
{
  Lesson::truncate();

  Eloquent::unguard();

  // $this->call('UserTableSeeder');
  $this->call('LessonsTableSeeder');
}

7、create new file app/models/Lesson.php and update:

<?php
class Lesson extends \Eloquent{
  protected $fillable = ['title', 'body'];
}

7、execute command to generate dummy data

$ php artisan db:seed
Screen Shot 2014-05-13 at 9.48.31 PM