Create Dummy Data Using Laravel Tinker

In the article, we will learn how to generate and insert test or fake or dummy data into the database using Laravel 8 factory. Not just that we will also learn how to use the PHP Faker library in the Laravel app to generate fake data.

Faker is an exorbitantly powerful PHP library that generates fake data for you and gives you moksha from all the pain you feel while developing a new application.

Create Laravel Project

composer create-project --prefer-dist laravel/laravel blog

Make Database Connection

Go to your project directory and find .env file and update it like :

.env
DB_CONNECTION=mysql
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

Make Model & Run Migration

Read also Laravel 8.0 Auth with Inertia JS Jetstream Example

Run below command to make model and make migration file

php artisan make:model Post -m

Add table properties in app/Models/Post.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
      'title',
      'url',
      'category'
      'body'
    ];
}

Place table properties in database/migrations/#########_create_blogs_table.php migration file as well:

Read also: Laravel 8.0 Get Country, City & Address From IP Address example

<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->string('url');
            $table->string('category');
            $table->timestamps();
        });
    }

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

Migrate table

php artisan migrate

Create Custom Factory

Working with laravel factory, we need to first create a custom factory class.

php artisan make:factory PostFactory --model=Post

Place the below code in database\factories\PostFactory.php file:

<?php

namespace Database\Factories;

use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class BlogFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $this->faker->name,
            'url' => Str::slug($this->faker->name),
            'category' => $this->faker->name,
            'body' => $this->faker->text,
        ];
    }
}

Generate Dummy Records with Factory Tinker

Go to terminal and run below command to go tinker console.

php artisan tinker

Generate dummy Data

Post::factory()->count(1000)->create()

Next, see the generated Post records inside PHPMyAdmin.

Remember, we are generating the data for Post model so we used Post::factory(), likewise append the same “Modal name” for which we are generating the dummy data. For instance we can do the following for User model:

User::factory()->count(500)->create()

In this tutorial, we created the limited properties, however, you can generate more properties with Faker:

  • Lorem Ipsum Text
  • Person
  • Address
  • Phone Number
  • Company
  • Real Text
  • Date and Time
  • Internet
  • User Agent
  • Payment
  • Color
  • File
  • Image
  • Uuid
  • Barcode
  • Miscellaneous
  • Biased
  • Html Lorem

Hope it helps you!