Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bwrigley's avatar

Seeder factory->each() updates all records not just new ones

Hi There,

I think I have misunderstood something basic here.

I have two database seeders. One to set up 10 random normal users and another to create two specific admin users.

The first one runs fine as expected.

The second one runs but updates all the first ones to make them admins.

 class AdminUsersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      collect([
        [
            'first_name' => 'Jim',
            'last_name' => 'Sanders',
            'email' => '[email protected]',
            'notification_frequency' => 'instant',
            'notification_method' => 'none',
            'stripe_id' => NULL,
            'card_brand' => NULL,
            'card_last_four' => NULL,
        ],
        [
            'first_name' => 'Michael',
            'last_name' => 'Johnson',
            'email' => '[email protected]',
            'notification_frequency' => 'instant',
            'notification_mobile' => NULL,
            'stripe_id' => NULL,
            'card_brand' => NULL,
            'card_last_four' => NULL,
        ],
      ])->each(function (array $userDetails) : void {

        factory(User::class)
            ->create($userDetails)
            ->each(function($user) {
                dump($user->id);
                $user->isAdmin(true);
        });
      });

    }

The dump line dumps out the id of every user in the database, twice! I assumed that factory()->create()->each() would only iterate through the models created by the factory?

I'm sure I'm missing something obvious. Any help is much appreciated!

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

There is no need for second each inside since you create a single user every iteration of the Collection:

collect([
    [
        'first_name' => 'Jim',
        'last_name' => 'Sanders',
        'email' => '[email protected]',
        'notification_frequency' => 'instant',
        'notification_method' => 'none',
        'stripe_id' => null,
        'card_brand' => null,
        'card_last_four' => null
    ],
    [
        'first_name' => 'Michael',
        'last_name' => 'Johnson',
        'email' => '[email protected]',
        'notification_frequency' => 'instant',
        //        'notification_mobile' => null,
        'stripe_id' => null,
        'card_brand' => null,
        'card_last_four' => null
    ]
])->each(function (array $userDetails): void {
    $user = factory(App\User::class)->create($userDetails);
    $user->isAdmin(true);
});

I expect the problem with every user becoming an admin might be because of the implementation of isAdmin()

bwrigley's avatar

@tykus

Thank you! Of course, that makes perfect sense. I should have seen that. Thanks again!

Please or to participate in this conversation.