Create Dummy Data Using Laravel Tinker

Websolutionstuff | May-21-2020 | Categories : Laravel PHP

In this example we can see how to add multiple dummy records in the database at a time using tinker and factory, mostly laravel tinker command is used for testing purposes, whenever developers are developing an application then they need to test many modules like insert update delete is working or not, pagination is working or not, filters are working properly and other functionalities. Also, we will see how to create dummy data using tinker and factory in laravel 6/7.

So, let's see how to create dummy data using tinker in laravel 6/7, laravel tinker command, laravel factory, laravel dummy data, how to add dummy data in laravel 6/7, insert record using tinker and factory in laravel 7, how to create fake data in laravel, laravel 6/7 create test data, laravel fake data generator.

So, in all these modules we can not add data manually every time it is a very boring task to add record one by one, but laravel provide a very useful command that is tinker and factory using this command we can add multiple dummy data at a time without using route or any controller, just type some code of command and that's it you can found a bunch of records in your database.

To add dummy users in the database, laravel provides default UserFactory.php in your application folder database\factories\UserFactory.php

<?php

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

 

 

So, if you want to create dummy users then you need to run the below command.

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

Using this command 200 dummy records are inserted automatically.

If you want to create dummy records for other tables in production for testing purposes then you need to add a new factory model for that and need to run tinker command for this. for this we are adding some code like below for example.

?php

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

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

In the above code, I have added a factory for the Blog model and I have added 2 fields for testing, 

 

 

Faker is used to generating many data types I have added a few of these below

  • Numbers
  • Lorem text
  • Person i.e. titles, names, gender, etc.
  • Addresses
  • Text
  • DateTime
  • Colour
  • Files
  • Images

For more datatype and details please read the Laravel Faker Documentation.

After adding code like the above, we will generate dummy records for the blogs table. Run the below command in your terminal

php artisan tinker
factory(App\Blog::class, 50)->create();

After running this command 50 records are added to your database.

 


You might also like :

Recommended Post
Featured Post
How Generate PDF From HTML View In Laravel
How Generate PDF From HTML Vie...

In this example, I will teach you how to generate PDF files from an HTML view in laravel. For generating PDF files I wil...

Read More

Jun-01-2020

How to Send Bulk Mail Using Queue in Laravel 9
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using queue in laravel 9. Laravel queue is used for sending bulk...

Read More

Mar-15-2022

Laravel 8 QR Code Generator Example
Laravel 8 QR Code Generator Ex...

In this tutorial, we will see the laravel 8 QR code generator example. we will generate QR code using simpleso...

Read More

Jan-24-2022

How to Upgrade PHP 8.0 to 8.1 in Ubuntu
How to Upgrade PHP 8.0 to 8.1...

Hey there! I recently needed to upgrade my PHP version from 8.0 to the latest 8.1 on my Ubuntu server. It's a smart...

Read More

Nov-03-2023