Create Dummy Data Using Laravel Tinker

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

In this example we can see hows to add multiple dummy records in database at a time using tinker and factory, mostly laravel tinker command is use for testing purpose, whenever developers are devloping application then thay need to test many modules like insert update delete is working or not, pagination is working or not , filters are working propely and other functionalites.

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

For add dummy users in database laravel provide 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 below command

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

Using this command 200 dummy records inserted automatically.

If you want to create dummy records for other table in production for testing purpose then you need to add new facory 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 above code i have added factory for Blog  model and i ahve added 2 field for testing, 

Faker is used to generate many data types i have added few of this as 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 ading code like above we will generate dummy records for blogs table. Run below command in your terminal

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

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

 

Featured Post



Follow us
facebooklogo github instagram twitter