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

10

36

I get the title error when I run command:

php artisan db:seed

My screenshot: enter image description here

I have no idea where this problem comes from. I was searching for code examples and solution but I haven't found anything :(

ArticlesTableSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
// use Laracasts\TestDummy\Factory as TestDummy;

class ArticlesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Models\Article::class, 30)->create();
    }
}

ArticleFactory.php

<?php

namespace Database\Factories;

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

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $faker->text(50),
            'body' => $faker->text(200)
        ];
    }
}

DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(ArticlesTableSeeder::class);
    }
}

Thank you in advance for your help!

Stormi answered 9/9, 2020 at 17:10 Comment(1)
you don't use the factory helper function anymore it would seem ... you should check the documentation on how to call these factories laravel.com/docs/8.x/database-testing#using-factoriesSwap
B
65

In laravel 8 the default route namespace was removed.

Try to change:

ArticlesTableSeeder.php:

 factory(App\Models\Article::class, 30)->create();

to:

\App\Models\Article::factory()->count(30)->create(); 

ArticleFactory.php:

protected $model = App\Models\Article::class;

to:

protected $model = \App\Models\Article::class;

and you will probably have to change:

 'title' => $faker->text(50),
            'body' => $faker->text(200)

to:

 'title' => $this->faker->text(50),
        'body' => $this->faker->text(200)
Boeotia answered 15/9, 2020 at 13:20 Comment(1)
You should have mentioned that the helper method factory() was removed in Laravel 8Canikin
U
17

All suggestions that were mentioned here are correct. Sometimes running composer require laravel/legacy-factories might fix your problem if you're using Laravel 8. Also, in case you get an error that says Class 'Database\Factories\ArticleFactory' not found then make sure you have class ArticleFactory extends Factory and not ModalFactory. And make sure you're using HasFactory in the Article Model like here.

<?php

namespace App\Models;

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

class Article extends Model
{
    use HasFactory;
}

For more info: Laravel 8 Modal Factories

Understand answered 19/9, 2020 at 21:47 Comment(0)
S
11

Try to change

factory(App\Models\Article::class, 30)->create();

to

App\Models\Article::factory()->count(30)->create();
Sunrise answered 11/9, 2020 at 13:9 Comment(0)
P
8

ArticlesTableSeeder.php

<?php

namespace Database\Seeders;

use App\Models\Article;
use Illuminate\Database\Seeder;

class ArticlesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Article::factory()->times(30)->create();
    }
}

ArticleFactory.php

<?php

namespace Database\Factories;

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

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $this->faker->text(50),
            'body'  => $this->faker->text(200)
        ];
    }
}
Plump answered 17/9, 2020 at 8:27 Comment(0)
D
4

If you are using version 8 of laravel, look at the upgrade guide. "Laravel's model factories feature has been totally rewritten to support classes and is not compatible with Laravel 7.x style factories. However, to ease the upgrade process, a new laravel/legacy-factories package has been created to continue using your existing factories with Laravel 8.x. You may install this package via Composer:

composer require laravel/legacy-factories"

https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces

Dimarco answered 15/9, 2020 at 14:23 Comment(0)
J
2

change

factory(App\Models\Article::class, 30)->create(); ‍

to

\App\Models\Article::factory(30)->create();

Jackass answered 11/9, 2021 at 5:3 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gradualism
K
1

I'm facing the same issue when I realized my model was not using the HasFactory trait.

<?php

namespace App\Models;

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

class Category extends Model
{
    use HasFactory;
    
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'categories';
   
}

So make sure this trait is being used by your model.

Kinesthesia answered 25/9, 2021 at 16:28 Comment(1)
This was exactly my problem, thank you! It isn't as clear as it could be in the docs (for me, at least) because I normally look at the code snippets to see what I have to include, whereas in this case it was mentioned in the body of the docs. laravel.com/docs/8.x/…Fino
B
-1

If you are using version 8 of laravel you can directory use:

use App\Models\Article; Article::factory()->count(30)->create();

Brubeck answered 30/10, 2021 at 19:48 Comment(0)
C
-1
  1. composer require laravel/legacy-factories
  2. php artisan db:seed
Cleavage answered 1/10, 2022 at 10:32 Comment(0)
C
-1

Use

$this->faker->name() instead of

fake()->name() method.

Cirque answered 6/9, 2023 at 17:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.