laravel seeder is giving an error and won’t seed

Tags: , ,



I’ve been trying to get my seeder to work but it keeps giving me the following error

 Call to undefined function DatabaseSeedersfactory()

  at database/seeders/ContactTableSeeder.php:16
     12▕      * @return void
     13▕      */
     14▕     public function run()
     15▕     {
  ➜  16▕         factory('AppModelsContact', 100)->create()
     17▕         ->each(function($contact) {
     18▕             $contact->addresses()->save(
     19▕                 (factory(AppAddress::class)->make())
     20▕             );

      +24 vendor frames 
  25  artisan:37
      IlluminateFoundationConsoleKernel::handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))

My DatabaseSeeder

<?php

namespace DatabaseSeeders;

use IlluminateDatabaseSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        //Model::unguard(); // Disable mass assignment

        $this->call(ContactTableSeeder::class);

        //Model::reguard();
    }
}

My ContactTableSeeder

<?php

namespace DatabaseSeeders;

use IlluminateDatabaseSeeder;

class ContactTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory('AppModelsContact', 100)->create()
        ->each(function($contact) {
            $contact->addresses()->save(
                (factory(AppAddress::class)->make())
            );
        });
    }
}

My ContactFactory

<?php

/* @var $factory IlluminateDatabaseEloquentFactory */

use AppModelsContact;
use FakerGenerator as Faker;

$factory->define(Contact::class, function (Faker $faker) {
    return [
            'firstName' => $faker->firstName,
            'lastName' => $faker->lastName,
            'email' => $faker->unique()->email,
            'phone' => $faker->phoneNumber,
            'birthday' => $faker->date($format = 'Y-m-d', $max = 'now')
        ];
    });

My AddressFactory

<?php

/* @var $factory IlluminateDatabaseEloquentFactory */

use AppModelsAddress;
use FakerGenerator as Faker;

$factory->define(Address::class, function (Faker $faker) {
    return [
        'number'    => $faker->number,
        'street'    => $faker->streetName,
        'city'      => $faker->city,
        'state'     => $faker->state,
        'zip'       => $faker->postcode,
        'type'      => 'home',
        'contact_id'=> factory(AppModelsContact::class),
    ];
});

Contact Model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;

use IlluminateDatabaseEloquentModel;

class Contact extends Model
{
    use HasFactory;

    protected $fillable = [ 'firstName', 'lastName', 'email', 'phone', 'birthday' ];

    public function addresses()
    {
        return $this->hasMany('AppModelsAddress');
    }
}

My Address Model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Address extends Model
{
    use HasFactory;
    protected $fillable = [ 'id', 'number', 'street', 'city', 'state', 'zip', 'type', 'contact_id' ];

    public function contacts()
    {
        return $this->belongsTo('AppModelsContact');
    }
}

I have tried running composer dump-auto composer update

None of those normal fixes seem to work.

I really have no idea why its failing Thanks in advance for your help

Answer

In Laravel8, you need to update your factory files like below or add laravel/legacy-factories package to your project. Further info: https://laravel.com/docs/8.x/upgrade#model-factories

<?php

namespace DatabaseFactories;

use AppModelsContact;
use IlluminateDatabaseEloquentFactoriesFactory;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'firstName' => $this->faker->firstName,
            'lastName' => $this->faker->lastName,
            'email' => $this->faker->unique()->email,
            'phone' => $this->faker->phoneNumber,
            'birthday' => $this->faker->date($format = 'Y-m-d', $max = 'now')
        ];
    }
}

In seeders use factory like that:

<?php

namespace DatabaseSeeders;

use IlluminateDatabaseSeeder;

class ContactTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        AppModelsContact::factory()->create(100)
            ->each(function($contact) {
                $contact->addresses()->save(
                    AppAddress::factory()->make()
                );
        });
    }
}


Source: stackoverflow