Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

How to quickly generate data using faker and tinker ?

All Laravel applications include Tinker, a REPL powered by the PsySH package. Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command, In this tutorial we will discuss How to quickly generate data using faker and tinker.

Step 1: Create table for inserting dummy data:

php artisan make:migration create_tinker_table_example

After this command update your migration file

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTinkerTableExample extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tinker_table_example', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('book_name');
            $table->string('book_author');
            $table->longText('book_description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tinker_table_example');
    }
}

Run Command : php artisan migrate

How to quickly generate data using faker and tinker ?

Step 2: Make a model:

Run Command : php artisan make:model TinkerTest

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TinkerTest extends Model
{
    protected $table = 'tinker_table_example';
}

Step 3: Go to database\factories\UserFactory.php:

Define your TinkerTest model into the factories and you can insert data according to you

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\User;
use App\TinkerTest;
use Illuminate\Support\Str;
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(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\TinkerTest::class, function (Faker $faker) {
		return [
		'book_name' => $faker->sentence,
		'book_author' => $faker->sentence,
		'book_description' => $faker->paragraph(30), 
	];
});


Step 4: After these steps go to your cmd :

Run command : php artisan tinker

php artisan tinker

>>> Factory('App\TinkerTest',20)->create()

Output: so we learned How to quickly generate data using faker and tinker

Read also :
How to create database seeder in Laravel 5.8?

How to quickly generate data using faker and tinker ?