Creating Dummy Data with Laravel 5

Created on August 13th, 2015

In this quick video we show how to use the faker package to create dummy data for your application.

You can head over to the github page to learn more about all the dummy data that's available. https://github.com/fzaninotto/Faker

Below you can find all the terminal commands and the code we used throughout this video.

Creating a New Laravel app. (requires the laravel installer: http://laravel.com/docs/5.1#installation)

laravel new faker

We then copy the .env.example to .env and generate the laravel encryption key:

cp .env.example .env
php artisan key:generate

Then, here is the PHP code that we added to our app/Http/routes.php file to display a name and an image:

Route::get('playground', function(){
	$faker = Faker\Factory::create();

	for($i=0; $i<50; $i++){
		echo $faker->name;
		echo '<img src="/admin/videos/edit/' . $faker->imageUrl(500, 300, 'city') . '" alt="" />';
		echo '<br />';
	}
});

Next, if you wanted to create the users table, inside of your project in terminal you need to execute the following command to create the users table

php artisan migrate

Then, if you wish to add some dummy data to your users table you can add modify the current 'playground' route to look like the following:

Route::get('playground', function(){
	$faker = Faker\Factory::create();

	for($i=0; $i<50; $i++){
		$user = array('name' => $faker->name, 'email' => $faker->email, 'password' => \Hash::make('password'));
		User::create($user);
	}
});

BTW, don't forget to use the correct user class, by adding the following to your routes.php file:

use App\User as User;

and that's it!

Please post any questions or suggestions below :)

Comments (0)