Laravel – Create Dummy Data using tinker factory
In this post we will give you information about Laravel – Create Dummy Data using tinker factory. Hear we will give you detail about Laravel – Create Dummy Data using tinker factoryAnd how to use it also give you demo for it if it is necessary.
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(AppUser::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 FakerGenerator 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(AppUser::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(AppPost::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(AppProduct::class, 500)->create();
So i hope you created your 500 dummy records on products table.
I hope it can help you…
Hope this code and post will helped you for implement Laravel – Create Dummy Data using tinker factory. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs