How to generate dummy data in laravel 5 [ Updated 2019 ]

How to generate dummy data in laravel 5 [ Updated 2019 ]

How to generate dummy data in laravel 5 [ Updated 2019 ]



To generate a seeder, we need to execute some commands. So lets see in this guide we are going to add dummy content in posts. Using laravel seeder.It is required that we have laravel project setup and database table for posts ready.





So if the project and database table is ready we can run commands. Open terminal or cmd and navigate to project folder. Then run command as given below



php artisan make:seed PostsTableSeeder

[ads]
It should look like this
How to generate dummy data in laravel 5 [ Updated 2019 ]





Now go to project folder and Database/seeds. Here you will see your file with name “PostsTableSeeder.php”.
Open it with any text editor you prefer.



In here in run function we need to add some code.







use Illuminate\Database\Seeder;

//import faker library
use Faker\Factory;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //reset the posts table
        DB::table('posts')->truncate();

        //generate 10 dummy posts

        $posts = [];
        //creating object from faker Factory
        $faker = Factory::create();

        for ($i=0; $i <=10 ; $i++) {

          $date = date("Y-m-d H:i:s",strtotime("2019-05-8 08:00:00 +{$i} days"));
          $posts[] = [
            'title'=>$faker->sentence(rand(8,13)),
            'content'=>$faker->paragraphs(rand(200,250),true),
            'category'=>rand(8,13),
            'tabs'=>$faker->sentence(rand(8,13)),
            'created_at'=>$date,
            'updated_at'=>$date,
          ];
        }
        //adding data to database
        DB::table('posts')->insert($posts);
    }
}


[ads]
In above code First we are including “use Faker\Factory;” to get access of faker library. we can see that we are resetting posts table and then we are adding posts using “Faker” feature of laravel. To know more about faker and commands available in faker you need to check here https://github.com/fzaninotto/Faker



In above code for loop will run 10 times and each time faker will generate different data and will insert it to database. We are using a php function “rand”. Which is responsible for generating random numbers . So rand(8,9) will generate 8 to 9 letters randomly.
after this again go to





Database/seed here open file “DatabaseSeeder.php”. And add your seeder file name in run function like this



      public function run()
    {
        // $this->call(UsersTableSeeder::class);
        $this->call(PostsTableSeeder::class);
    }

[ads]
Now run below given command in terminal to get dummy content in database table





      php artisan db:seed

[ads]
It should be like this



At this point go to database and check dummy content in database table.





https://18.191.183.98/2019/04/09/how-to-setup-laravel-on-macos-xampp-2019/How to generate dummy data in laravel 5 [ Updated 2019 ]