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

vincent15000's avatar

Factory recycling doesn't work while seeding the database

Hello,

It's a personal project and I have this code.

class TransactionFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'slug' => Str::ulid(),
            'date' => fake()->dateTimeThisMonth('-30 days'),
            'name' => fake()->words(2, true),
            'amount' => fake()->randomFloat(2, 1, 300),
            'is_revenue' => fake()->boolean(),
            'category_id' => Category::factory(),
            'paymode_id' => Paymode::factory(),
            'account_id' => Account::factory(),
            'user_id' => User::factory(),
        ];
    }
}
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call([
            UserSeeder::class,
            AccountSeeder::class,
            CategorySeeder::class,
            PaymodeSeeder::class,
            TransactionSeeder::class,
        ]);
    }
}
class TransactionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $categories = Category::all(); // seems to not work
        $paymodes = Paymode::all(); // seems to not work
        $accounts = Account::all(); // seems to not work
        $users = User::all();

		dd($categories); // empty, but in phpmyadmin, several categories are stored

        Transaction::
            factory()
            ->count(40)
            ->recycle($categories)
            ->recycle($paymodes)
            ->recycle($accounts)
            ->recycle($users)
            ->create();
    }
}

The transactions are created, but while creating, it doesn't reuse the existing categories, paymodes and accounts. Just the users are reused.

I have also tried ->recycle($categories, $paymodes, $accounts, $users) and even as an array passed to the recycle method, but none of these solutions work.

Hmmm ... I have checked in the database : the categories, paymodes and accounts are created from the category, paymode and account seeders. But several other categories, paymodes and accounts are created via the transaction seeder.

But if I try dd($categories) inside the seeder, the collection is empty.

So I suppose that in the last versions of Laravel, the seeders are perhaps in a database transaction ? Any other suppositions ?

Can you help me please ?

Thanks a lot.

V

0 likes
2 replies
vincent15000's avatar

I just checked something else.

When I connect to MariaDB with PHPMyAdmin, there are accounts, paymodes, ... but with Tinker, the tables seems to be empty.

Perhaps a database problem.

So I have checked the database configuration (in the .env file) and all seems to be correctly defined.

What do you suggest me to check ?

Please or to participate in this conversation.