Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple factories not resetting each time #13534

Closed
SteveEdson opened this issue May 12, 2016 · 4 comments
Closed

Multiple factories not resetting each time #13534

SteveEdson opened this issue May 12, 2016 · 4 comments

Comments

@SteveEdson
Copy link

In my seeder I'm calling a factory on a class multiple times. Each time, I only create 1 new model, however, my ->each() function is carrying across models created from the previous calls.

E.g.

factory(App\Team::class, 1)->create([
   'name' => 'Mercedes'
])->each(function($team) {
    echo "Factory 1. Created team " . $team->name . "\n";

    $team->drivers()->create(factory(App\Driver::class)->make(['name' => 'Lewis Hamilton', 'type' => 'primary'])->toArray());
    $team->drivers()->create(factory(App\Driver::class)->make(['name' => 'Nico Rosberg', 'type' => 'primary'])->toArray());
});

factory(App\Team::class, 1)->create([
   'name' => 'McClaren'
])->each(function($team) {
    echo "Factory 2. Created team " . $team->name . "\n";

    $team->drivers()->create(factory(App\Driver::class)->make(['name' => 'Jenson Button', 'type' => 'primary'])->toArray());
    $team->drivers()->create(factory(App\Driver::class)->make(['name' => 'Fernando Alonso', 'type' => 'primary'])->toArray());
});

Factory 1. Created team Mercedes
Factory 2. Created team Mercedes
Factory 2. Created team McClaren

Using v5.2.31

@GrahamCampbell
Copy link
Member

I don't see how this can be happening. It sounds more like a bug in your code. Feel free to discuss on the forums, and come back here if you have really small case that we can reproduce on a stock app.

@SteveEdson
Copy link
Author

Just managed to replicate the issue on a fresh install using laravel new test:

Model Factory:

$factory->define(App\Team::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->word,
        'primary_colour' => $faker->hexColor
    ];
});

DatabaseSeeder.php

    public function run()
    {
        $this->call(TeamSeeder::class);
    }

Migration (SQLite)

        Schema::create('teams', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('primary_colour');
            $table->timestamps();
        });

Model

class Team extends Model
{
    //
}

Team Seeder

class TeamSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(Team::class, 1)->create()->each(function() {
            echo "Seeder 1";
        });

        factory(Team::class, 1)->create()->each(function() {
            echo "Seeder 2";
        });
    }
}

Output

Seeder 1Seeder 2Seeder 2Seeded: TeamSeeder

Also worth noting, each time I run the seeder:

~/Repos/ltest  art db:seed
Seeder 1Seeder 1Seeder 1Seeder 2Seeder 2Seeder 2Seeder 2Seeded: TeamSeeder
~/Repos/ltest  art db:seed
Seeder 1Seeder 1Seeder 1Seeder 1Seeder 1Seeder 2Seeder 2Seeder 2Seeder 2Seeder 2Seeder 2Seeded: TeamSeeder

@GrahamCampbell
Copy link
Member

Ping @taylorotwell

@JosephSilber
Copy link
Member

This is not a bug. Calling create witha single model does not return a collection; it return the model instance. A model doesn't have an each method, so it delegates to the builder.

Just don't call each here. It's wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants