Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TinkerTest extends Model
{
protected $table = 'tinker_table_example';
}
<?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),
];
});
php artisan tinker
>>> Factory('App\TinkerTest',20)->create()
Read also :
How to create database seeder in Laravel 5.8?