Laravel 11 Generate Fake Data using Factory Tinker Example

By Hardik Savani April 16, 2024 Category : Laravel

In this article, I will show you how to generate fake data using factory tinker in laravel 11 application.

As we know, testing is a very important part of any web development project. Sometimes we may require to add hundreds of records to our users table, or maybe thousands of records. Also, think about if you require to check pagination, then you have to add some records for testing. So what will you do at that moment? Will you manually add thousands of records? What do you do? If you add manually thousands of records, then it can take more time.

However, Laravel has a tinker that provides the ability to create dummy records for your model table. So in the Laravel application, they provide a User model factory created by default. You can see how to create records using the factory below:

laravel 11 generate fake data

Generate Dummy Users:

php artisan tinker
    
User::factory()->count(5)->create()

This by default created a factory of Laravel. You can also see that at the following URL: `database/factories/UserFactory.php`.

Create Custom Factory:

But when you need to create dummy records for your products, items, or admin table, then you have to create a new factory using the tinker command. Here, I will give you a simple example of creating a product factory, and you will understand how it works. So let's create a Product Model as shown below:

app\Models\Product.php

<?php
    
namespace App\Models;
    
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
    
class Product extends Model
{
    use HasFactory;
    
    protected $fillable = [
        'name', 'slug', 'detail'
    ];
}

Now let's create our custom factory using the below command:

php artisan make:factory ProductFactory --model=Product

Now they created a new Factory class for products, and you can add the code below:

database\factories\ProductFactory.php

<?php
  
namespace Database\Factories;
  
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Product;
  
/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory
 */
class ProductFactory extends Factory
{
  
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Product::class;
      
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            'name' => $this->faker->name,
            'slug' => Str::slug($this->faker->name),
            'detail' => $this->faker->text,
        ];
    }
}

Using Faker, you can generate the following data types:

Numbers

Lorem text

Person i.e. titles, names, gender etc.

Addresses

Phone numbers

Companies

Text

DateTime

Internet i.e. domains, URLs, emails etc.

User Agents

Payments i.e. MasterCard

Colour

Files

Images

uuid

Barcodes

As you see above, you can simply use data types. Now we are going to create 500 records of the products table by following command:

Generate Dummy Product:

php artisan tinker

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

So I hope you've created your 500 dummy records in the products table.

Output:

I hope it can help you...

Shares