๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 12.x โ€บ eloquent-factories
Eloquent: Factories | Laravel 12.x - The clean stack for Artisans and agents
When testing your application or seeding your database, you may need to insert a few records into your database. Instead of manually specifying the value of each column, Laravel allows you to define a set of default attributes for each of your Eloquent models using model factories.
๐ŸŒ
Medium
medium.com โ€บ @trig79 โ€บ laravel-factory-set-up-and-factory-state-explained-f39f7f8eecae
Laravel Factory Set Up and Factory State Explained | by Trig | Medium
September 10, 2024 - Alright, letโ€™s start with the basics. Laravel Factories are like blueprints for your models. They let you create fake data for testing or seeding your database, without the headache of manually specifying every attribute.
Discussions

How to create factory for Product and Categories? | Laravel.io
The Laravel portal for problem solving, knowledge sharing and community building. More on laravel.io
๐ŸŒ laravel.io
eloquent - Laravel Factory - Stack Overflow
Hi I am new to creating factories on Laravel and I am setting up a blog website. There is no need for users at this time as it is a draft but I am trying to set up a factory to create dummy blog po... More on stackoverflow.com
๐ŸŒ stackoverflow.com
laravel - Why is php artisan make:factory not Generating with a Model - Stack Overflow
So long story short, I have a model in my Larvel 8 (Jetstream) application called Board. I am trying to generate a factory for this Board model. When I use either of the commands below: php artisan... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Laravel 8 Factories For Non-Models

I actually figured it out, it's kind of annoying but...works

On the class you want to have a factory for, besides adding the HasFactory trait, you need to override a newFactory() method that simply returns MyFactoryClass::new();

Hopefully that helps someone else out!

More on reddit.com
๐ŸŒ r/laravel
5
13
October 3, 2020
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 8.x โ€บ database-testing
Database Testing | Laravel 8.x - The clean stack for Artisans and agents
First, let's talk about Eloquent model factories. When testing, you may need to insert a few records into your database before executing your test. Instead of manually specifying the value of each column when you create this test data, Laravel allows you to define a set of default attributes for each of your Eloquent models using model factories.
๐ŸŒ
Kinstaยฎ
kinsta.com โ€บ home โ€บ resource center โ€บ blog โ€บ laravel โ€บ how to generate and use fake records with laravel model factories
How To Generate and Use Fake Records with Laravel Model Factories
November 26, 2024 - Model factories define a set of default attributes for each of your Eloquent models. For example, if youโ€™re making a blogging app allowing authors and moderators to approve comments before they go live, youโ€™d need to test if the function works properly before deploying it to your users. All this requires test data. To test the blogging app described above, you need comments data to imitate and test your applicationโ€™s functionality. Laravel allows you to do that without getting comments from actual users by using Laravel factories and Faker to generate fake data.
๐ŸŒ
Laravel News
laravel-news.com โ€บ home โ€บ laravel tutorials โ€บ using laravel model factories in your tests
Using Laravel Model Factories in your tests - Laravel News
July 28, 2022 - We set a default for our factory so that all newly created posts are drafts. Then we add a method for setting the state to be published, which will use the correct Enum value and set the published date - a lot more predictable and repeatable in a testing environment.
๐ŸŒ
The Man in the Arena
carlalexander.ca โ€บ static-factory-methods-laravel
Using static factory methods with Laravel models | The Man in the Arena
July 2, 2020 - The rest of the static factory method just calls the generic Model::create method. We pass it an array with the attributes we want it to assign to the new BlogPost object. The array contains the ID of the user who authored the post and the title. For a lot of Laravel developers, the createFromUser method will be enough.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @zulfikarditya โ€บ the-power-of-laravel-factories-efficient-database-seeding-for-testing-and-development-f8700c1b2083
The Power of Laravel Factories: Efficient Database Seeding for Testing and Development | by Zulfikar Ditya | Medium
March 17, 2025 - Laravel Factories represent one of the frameworkโ€™s most powerful features for developers working with databases. At their core, factories are PHP classes that allow you to define a blueprint for generating fake model instances with realistic data.
๐ŸŒ
GitHub
github.com โ€บ laravel-shift โ€บ factory-generator
GitHub - laravel-shift/factory-generator ยท GitHub
This package generates model factories from existing models using the new class-based factories introduced in Laravel 8.
Starred by 50 users
Forked by 21 users
Languages ย  PHP
๐ŸŒ
GitHub
github.com โ€บ TheDoctor0 โ€บ laravel-factory-generator
GitHub - TheDoctor0/laravel-factory-generator: Model factory generator for Laravel ยท GitHub
Automatically generate factories from your existing models. It will allow you to write tests containing your models much faster. ... For Laravel 8.x and 9.x check the v1.3.2.
Starred by 214 users
Forked by 19 users
Languages ย  PHP 97.0% | Blade 3.0%
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 5.5 โ€บ database-testing
Database Testing | Laravel 5.5 - The clean stack for Artisans and agents
When testing, you may need to insert a few records into your database before executing your test. Instead of manually specifying the value of each column when you create this test data, Laravel allows you to define a default set of attributes for each of your Eloquent models using model factories.
๐ŸŒ
InMotion Hosting
inmotionhosting.com โ€บ inmotion hosting home โ€บ support โ€บ education channels โ€บ laravel โ€บ creating laravel database model factories
Creating Laravel Database Model Factories | InMotion Hosting
May 30, 2024 - This command creates a new User instance with the attributes defined in the factory. To create multiple instances of a model, use the count method: ... This command creates 10 instances of the User model. Laravel factories support states, which allow you to define different variations of model attributes.
Top answer
1 of 1
10

Try publishing your laravel stubs and confirm that the stub file contents are defined as expected.

  1. Publish the stubs.

php artisan stub:publish

  • This should create a /stubs folder in the root project directory.
  • Inside that folder, you will see all stubs.
  1. Most specifically, open the stub file called factory.stub
  • It's file contents should look something similar to this:
<?php

namespace {{ factoryNamespace }};

use Illuminate\Database\Eloquent\Factories\Factory;
use {{ namespacedModel }};

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            //
        ];
    }
}

Notes:

From the looks of it, it appears as if your current factory stub is missing the section(s) below:

// ...
use {{ namespacedModel }};
// ...

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

// ...

Ideally speaking, in normal (default) cases, running a command that generates a factory with a linked model should look like this:

Command:

php artisan make:factory BoardFactory --model=Board

Expected Output File (database/factories/BoardFactory.php):

<?php

namespace Database\Factories;

use App\Models\Board;
use Illuminate\Database\Eloquent\Factories\Factory;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            //
        ];
    }
}

Addendum:

As pointed out in a comment by @miken32, in Laravel versions published later than Oct 22, 2021, declaring a model attribute in your Factory class will no longer be necessary:

Laravel Pull Request

At this time, database factories have this hidden feature where database models can be "guessed".

So, this pull request proposes that remove protected $model from the Factory stub, as probably the current "guess" logic works for 99.99% of the people. In addition, I've also pull requested to the skeleton that we remove protected $model = User::class from the UserFactory.php: laravel/laravel#5713.

๐ŸŒ
InterServer
interserver.net โ€บ home โ€บ laravel โ€บ laravel factories: building realistic test data with laravel factory patterns
Laravel Factories: Building Realistic Test Data with Laravel Factory Patterns - Interserver Tips
September 11, 2023 - In the world of web development, testing is the bedrock upon which stable and reliable applications are built. Laravel, a popular PHP framework, offers a powerful tool known as โ€œFactoriesโ€ to streamline the process of generating test data.
๐ŸŒ
Medium
nickpoulos.medium.com โ€บ using-laravel-8-factory-classes-for-non-models-1f8c0b97764a
Using Laravel 8 Factory Classes For Non-Models | by Nick Poulos | Medium
October 3, 2020 - I like to use Factories for classes other than Eloquent models, especially something like a DTO (see here and here). In previous versions of Laravel (< 8) this was easy and straightforward to accomplish.
๐ŸŒ
Hypervel
hypervel.org โ€บ docs โ€บ eloquent-factories
Hypervel - A Coroutine PHP Framework for Laravel Artisans.
February 14, 2026 - As you can see, in their most basic form, factories are classes that extend Hypervel's base factory class and define a definition method.
๐ŸŒ
Aaron Saray
aaronsaray.com โ€บ 2024 โ€บ using-laravel-factory-states-to-add-more-configuration-after-creation
Using Laravel Factory States to Add More Configuration After Creation | Aaron Saray
User::factory()->admin()->create(['first_name' => 'Aaron']). Perfect! Now, it calls the admin() state which - instead of setting a state, still fluently returns, but this time registering a callback into afterCreating(). Now, after the user is created, as a normal user, activated, named Aaron, a final handler executes and adds the Role::ADMIN to them. Looking for more Laravel Tips & Tricks?