Laravel 7 - How to Create Dummy Data using laravel/tinker Package?

Today we will learn how to engender dummy records in your database table utilizing the tinker factory of Laravel 7. As we ken testing is a very consequential part of any web development project. Sometime we may require to integrate hundreds records in your users table, OR maybe thousands of records. Additionally, cogitate if you require to check pagination. then you have to integrate some records for testing. So what you will do it at that moment, You will integrate manually thousands of records? What you do ?. If you integrate manually thousands of records then it can take more time.

However, Laravel 7 has a composer package "laravel/tinker" that we will provide you to engender dummy records by utilizing their command. So Laravel 7 by default take "laravel/tinker" package for project. Additionally, they provide by default one factory for a utilizer table. You can check path here :

database/factories/. In this folder, you can add your different factory for a different model.

So here you can see how to generate dummy users by the following command because for User Model they provide default factory.

Generate Dummy Users :

php artisan tinker
factory(App\User::class, 500)->create();

After Run Above command, you will get 500 records on your users' table. So it's very simple. you can also create thousands of records in seconds. So it's pretty easy.

Create Dummy Records For Product:

As you see above command for a user, But you can not do the same for another model. If you want to do this then you have to add a factory for that model. So I am going to give you a full example of factory from scratch.

I have a Product model with a products table. Now we want to add dummy records for the Product model. So we have to add factory-like as bellow :

database/factories/UserFactory.php

<?php


use Faker\Generator as Faker;


/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/


$factory->define(App\User::class, function (Faker $faker) {
    static $password;
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];
});


$factory->define(App\Post::class, function (Faker $faker) {
    return [
        'title' => $faker->name,
        'body' => $faker->text,
    ];
});

I simply add Product factor for two fields.

Using Faker you can be used to generate the following data types:

  • Numbers
  • Lorem text
  • Person i.e. titles, names, gender etc.
  • Addresses
  • Phone numbers
  • Companies
  • Text
  • DateTime
  • Internet i.e. domains, URLs, emails etc.
  • User Agents
  • Payments i.e. MasterCard
  • Colour
  • Files
  • Images
  • uuid
  • Barcodes

As you see above you can simply use data types. Now we are going to create 500 records of products table by the following command:

Generate Dummy Product:

php artisan tinker
factory(App\Product::class, 500)->create();

So i hope you created your 500 dummy records on products table.

I hope you like this article.

Tags: