Laravel 5 - Create Dummy Data using tinker factory

By Hardik Savani | August 31, 2017 | | 1339 Viewer | Category : PHP Laravel


Hi, Friends

Today we will learn how to generate dummy records in your database table using tinker factory of Laravel 5.5. As we know testing is very important part of any web development project. Sometime we may require to add hundreds records in your users table, OR maybe thousands of records. Also think about if you require to check pagination. then you have to add some records for testing. So what you will do it at that that moment, You will add manually thousands of records ? What you do ?. If you add manually thousands of records then it can be take more time.

However, Laravel 5 have composer package "laravel/tinker" that we will provide you to generate dummy records by using their command. So Laravel 5.5 by default take "laravel/tinker" package for project. Also they provide by default one factory for user table. You can check path here : database/factories/. In this folder you can add your different factory for different model.

So here you can see how to generate dummy users by 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 a seconds. So it's pretty easy.

Create Dummy Records For Product:

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

I have Product model with products table. Now we want to add dummy records for 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 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 it can help you...



We are Recommending you: