Add Dummy Content Using Laravel + Faker


One part of testing any application is seeing how it reacts to a volume of data. When building an application with Laravel, we use a great package called Faker to make this process quick. Their API allows us to painlessly add a ton of content to our application.

This is an add-on to our previous post Dynamic Pagination using Laravel & AngularJS and will show you how to add 50 "fake" posts to your application for testing.

The tools we will use:

Let's Get Started

I will assume that you already have a database setup. I will also assume you've created a posts table with the following structure:

  • id
  • title
  • content

Install Faker

Our fist step will be to install Faker within our Laravel application. You'll need to use composer to install the package. Either through terminal or the composer.json file.

Using Terminal

  1. cd /Directory-of-Laravel-App
  2. sudo composer require fzaninotto/faker --dev

Using composer.json

  1. Open up your composer.json file.
  2. Add the following snippet to your require-dev array: "fzaninotto/faker": "1.3.*@dev"
  3. cd /Directory-of-Laravel-App
  4. Run the command composer update

Create Seeder File & Define Data

The next step is to create a file which will generate the fake data for us. Since we are trying to seed the posts table, lets go ahead and create a file called PostsTableSeeder.php inside app/database/seeds/. Faker adds on to the existing seeder class that ships with Laravel.

The best part of Faker is the flexability of its API. We can easily insert sentences, paragraphs, images, dates, names, phone numbers and more. A complete list of content types is available here.

As you'll see we opted to use the sentence (post title) and paragraph (post description) content types.


use Faker\Factory as Faker;

class PostsTableSeeder extends Seeder {

    public function run() {
        $faker = Faker::create();

        foreach(range(1, 50) as $index) {
                'title'     => $faker->sentence(5),
                'content'   => $faker->paragraph(4)

Add to Seeder Class

Our last step (I told you Faker made this quick), is to add the Posts seeder we just created to our master seeder class. Go ahead and edit the file DatabaseSeeder.php within app/database/seeds/.

Assuming you haven't made any changes to the existing class, your file should look like this:


use Illuminate\Database\Seeder;  
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

    public function run() {
        DB::table('posts')->truncate(); //If you want to reset to 50. See note below.

Now everytime you run php artisan db:seed in your terminal you will be adding 50 more posts to your database.

Note: If you don't want to keep adding 50 additional posts everytime you run db:seed you will want to include this line of code. It will remove the previous database entries before inserting the new dummy content.

Obviously we are using this library within a test enviroment. So when you move the application to production you will most certainly want to remove the truncate(); function from your code. Otherwise you may accidentaly run db:seed and erase data from your production database.