Laravel, Call to undefined function Database\Seeders\factory()

Laravel, Call to undefined function Database\Seeders\factory()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel, Call to undefined function Database\Seeders\factory()

If you are running php artisan db:seed and encounter a Call to undefined function Database\Seeders\factory() error, the issue almost certainly lies in your codebase using legacy syntax that was removed in recent Laravel versions. The specific culprit is the use of the global factory() helper function inside a seeder class.

In older iterations of Laravel (specifically 5.3 through 5.8), the testing framework introduced a Factory facade or helper that allowed developers to create model instances easily. Many early tutorials and boilerplates adopted this pattern:

use Illuminate\Database\Eloquent\Factory; // Or no import at all in older versions

public function run()
{
    factory(App\Models\Article::class, 30)->create();
}

However, Laravel introduced a significant architectural shift. The global factory() helper was deprecated and eventually removed because it conflicted with the new, more robust Eloquent Factories system introduced in Laravel 5.8. This new system is strictly typed, supports complex relationships out of the box, and integrates directly with the database seeder API via the $this->call() method or direct invocation.

When you see the error message indicating that Database\Seeders\factory is undefined, it means the autoloader cannot find a function named factory in the global namespace. The code is attempting to call a global helper that no longer exists by default in Laravel 6, 8, 9, and 10+.

Moving from Legacy to Eloquent Factories

To resolve this, you must migrate from the legacy factory() syntax to the modern Eloquent Factory class. This involves defining a factory file and referencing it explicitly.

First, generate a factory for your model using Artisan:

php artisan make:factory ArticleFactory --model=App\Models\Article

This creates a file at database/factories/ArticleFactory.php. You should update this file to define the state of your models. Based on your source material, you likely have a ModelFactory class defined in Database\Factories, but modern Laravel places these in database/factories. Ensure your factory definition looks like this:

namespace Database\Factories;

use App\Models\Article;
use Illuminate\Database\Eloquent\Factories\Factory;
use Faker\Factory as Faker; // Use the global Faker facade if needed

class ArticleFactory extends Factory
{
    protected $model = Article::class;

    public function definition(): array
    {
        return [
            'title' => (string) str(random_int(10, 50))->random(),
            'body' => (string) str(random_int(20, 100))->random(),
            // Add other columns here as needed
        ];
    }

    public function published(): static
    {
        return $this->state(fn (array $attributes) => [
            'published_at' => now(),
            'status' => 'published',
        ]);
    }
}

Next, update your ArticlesTableSeeder to instantiate this factory explicitly. You no longer need the global factory() function. Instead, you call the factory class directly:

namespace Database\Seeders;

use App\Models\Article;
use Illuminate\Database\Seeder;
use Database\Factories\ArticleFactory; // Ensure correct namespace

class ArticlesTableSeeder extends Seeder
{
    public function run(): void
    {
        ArticleFactory::count(30)->create();
        
        // Alternatively, if you want to use the faker helper directly within the factory:
        // $faker = Faker\Factory::create();
        // ArticleFactory::new()->define(['title' => $faker->text(50)]);
    }
}

Why the Shift Matters

The transition isn't just about fixing an error; it's about adopting a better pattern. The legacy factory() helper was loosely typed and often required manual instantiation of the Faker library or facade calls that could lead to namespace collisions. The new Eloquent Factory system uses strict typing (definition(): array), making the code safer and easier to read.

Furthermore, the new system handles the relationship between factories and models more elegantly. If your Article model has relationships like author() or comments(), the legacy factory often struggled with these without verbose configuration. The modern approach allows you to define relationships directly in the factory class using the $this->relationship() method, streamlining complex seeding tasks.

Final Steps

Once you have updated your seeder and ensured your factory class is correctly defined in the database/factories directory (or your custom Database\Factories namespace if you are maintaining legacy structure), clear your configuration cache to ensure no old bindings remain:

php artisan config:clear
php artisan cache:clear
php artisan db:seed --class=DatabaseSeeder

By replacing the deprecated global helper with explicit class calls, your application aligns with current Laravel standards. This ensures compatibility across versions and takes advantage of the improved type safety and relationship handling built into the Eloquent Factory system.

Our network