---
title: "How to Create Dummy Data for Testing using Factory in Laravel 11?"
url: "https://magecomp.com/blog/create-dummy-data-for-testing-using-factory-laravel-11/"
date: "2025-01-03T12:15:28+00:00"
modified: "2025-01-06T11:18:24+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "Laravel"
word_count: 475
reading_time: "3 min read"
summary: "Laravel is a popular PHP framework that comes with lots of features and makes development easier. One of the useful features is using a factory to generate dummy data."
description: "Learn how to create dummy data for testing in Laravel 11 using factories."
keywords: "Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Write Cleaner Laravel Code Using the when() Helper"
    url: "https://magecomp.com/blog/write-cleaner-laravel-code-using-when-helper/"
  - title: "CRUD Operation in Laravel 8: Step by Step Tutorial"
    url: "https://magecomp.com/blog/crud-operation-laravel-8/"
  - title: "How to Use Laravel Eloquent WHEREIN Query?"
    url: "https://magecomp.com/blog/use-laravel-eloquent-wherein-query/"
---

# How to Create Dummy Data for Testing using Factory in Laravel 11?

_Published: January 3, 2025_  
_Author: Bharat Desai_  

![How to Create Dummy Data for Testing using Factory in Laravel 11](https://magecomp.com/blog/wp-content/uploads/2025/01/How-to-Create-Dummy-Data-for-Testing-using-Factory-in-Laravel-11-1024x512.webp)

Laravel is a popular PHP framework that comes with lots of features and makes development easier. One of the useful features is using a factory to generate dummy data.

[![Laravel Development Service](https://magecomp.com/blog/wp-content/uploads/2025/01/Laravel-Development-Service-1024x284.webp)](https://magecomp.com/services/laravel-development-services/)Many times in Laravel we need test data for some functionality. Adding data manually takes a lot of time. To avoid that, Laravel provides us with a factory to generate test data. Laravel factory allows to create dummy or test data for testing purposes. Hence, using a factory you can test your application without real data.

## Steps to Create Dummy Data for Testing using Factory in Laravel 11:
### Step 1: Create a Model
Use the below command to create a model

```
php artisan make:model Book
```

```
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Book extends Model
{
    use HasFactory;
    protected $fillable = [
        'name', 'author', 'published_at', 'isbn'
    ];
}
```

### Step 2: Create a New Factory for Class Book
Use the below command to create a new factory for the class book

```
php artisan make:factory BookFactory
```

Go to the path below

**database\factories\BookFactory.php**

And add the code as follows

```
<?php
namespace DatabaseFactories;
use AppModelsBook;
use IlluminateDatabaseEloquentFactoriesFactory;
/**
 * @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsBook>
 */
class BookFactory extends Factory
{
    protected $model = Book::class;
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'name' => $this->faker->title,
            'author' => $this->faker->name,
            'published_at' => $this->faker->date,
            'isbn' => $this->faker->isbn13,
        ];
    }
}
```

[![Hire laravel Developer](https://magecomp.com/blog/wp-content/uploads/2024/12/Hire-Laravel-Expert-Now-3-1024x284.webp)](https://magecomp.com/services/hire-laravel-developer/)

### Step 3: Test Output
Run the below commands for output

```
php artisan tinker
Book::factory()->count(10)->create()
```

![](https://magecomp.com/blog/wp-content/uploads/2025/01/generate-dummy-data-using-laravel-factory-1024x518.webp)

## Conclusion
Laravel 11 enhances the developer experience with its robust and intuitive factories. By following the steps in this guide, you can generate realistic dummy data efficiently, improving the reliability of your testing and development processes.

If you have any questions or additional tips about using factories in Laravel 11, feel free to share them in the comments!

[![Contact Us](https://magecomp.com/blog/wp-content/uploads/2024/12/Contact-Us-3-1024x284.webp)](https://magecomp.com/contact)Get assistance from **[Laravel expert developers](https://magecomp.com/services/hire-laravel-developer/)** for your web application.

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/create-dummy-data-for-testing-using-factory-laravel-11/](https://magecomp.com/blog/create-dummy-data-for-testing-using-factory-laravel-11/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-15 14:39:36 UTC_  
