TECHNIG
Gateway for IT Experts and Tech Geeks

How to Use Faker and Seeds in Laravel 5?

Developing application with Laravel framework has become very easy. If you want to develop any web application, you will definitely refer to Laravel as PHP developer. Beside other amazing features that Laravel 5 gives us, here is something called Faker Seeds in Laravel 5. In this short tutorial will learn what Faker Seeds in laravel 5 is, and how to use them in our project to speed up our development. 5

What is Faker Seeds in Laravel 5?

Basically, Faker is a PHP library that generates fake data for you and Seed is a class in Laravel for seeding test data to your database. Faker is just a library, it’s not only for Laravel, you can use it on any other PHP projects as well. Even if you develop a small application, you will need some test data to work with. So Faker and is the best library to generate random data for your database.

How to use Faker seeds in laravel 5 ?

Now, how to use it in our Laravel projects. The good news is this that it’s already included in every laravel 5 project and you don’t need to worry about any extra work or installation.  All seed classes are stored in database/seeds directory.

Imagine if you want to generate some 5 to 10 post for your blog project, you will create a seed class in your seed directory. You can give your class any name, but it’s better to be sensible convention. To create a seed file, you can create it manually, as well as using artisan command. 

To create a seeder class for our post, open command line and navigate to your project directory. Now write the following command.

php artisan make:seeder PostsTableSeeder

Now you have a File called PostsTableSeeder.php seeds folder. Open the file and you will see the following codes.

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

Probably, you one line of code is missing from your code and that is the second line which says : We are using Fakder library in this class.

Generating Test Data.

Now, lets generate some test data. To do so, you must run the seeder, but before running the seeder, you must add some extra lines of code to your seeder class. Inside run function, you must specify the column name with type of data you want to put in.

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      $faker = Faker::create();
      foreach (range(1,5) as $index) {
        DB::table('posts')->insert([
            'title' => $faker->catchPhrase,
            'content' => $faker->paragraph,
            'created_at' => $faker->dateTime($max = 'now'),
            'updated_at' => $faker->dateTime($max = 'now'),
        ]);
      }
    }
}

What the above code do?

First we have created an instance of Faker class to the variable $faker. Now we can use the $faker object to access the types of data we want to insert in database. There are many types of data that you can explore in Faker Documentation here. We have just used some basic types like catchPhrase, paragraph and datetime. The foreach will loop 5 times and generate 5 record in database which you can increase or decrease those numbers.

So once you wrote the necessary code for your database table, you can run it through php artisan.

php artisan db:seed --class=PostsTableSeeder

The above command will Generate 5 posts for your posts table in the database.

Conclusion

You will not learn anything unless you practice it. So if you want to master in Laravel or any other things, you must start doing it practically. Of course there is a lot that you can do with Faker and seeder in Laravel, but is was just the basics. I hope you have learned something from this tutorial, till next time keep practicing.

2 Comments
  1. Alex Morgan says

    Good Article. Nice to find it easy.

    1. Hujjat Nazari says

      glad it helped you 🙂

Leave A Reply

Your email address will not be published.