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

Kirk.Franklin's avatar

Laravel seeder only inserting one record

I have a Laravel seeder that is only inserting one record when I have a factory and the seeder should insert 2500 records.

Factory:

    public function definition(): array
    {
        $timestamp = $this->getFakeTimestamp();

        return [
            'title' => $this->faker->sentence(),
            'body' => $this->faker->paragraph(),
            'more_inside' => $this->faker->paragraph(),
            'subsite_id' => (new Subsite())->inRandomOrder()->first(),
            'user_id' => (new User())->inRandomOrder()->first(),
            'created_at' => $timestamp,
            'published_at' => $timestamp,
            'updated_at' => null,
            'deleted_at' => null,
            'is_published' => true,
            'state' => PostStateEnum::Published->value,
        ];
    }

Seeder:

final class FakePostSeeder extends Seeder
{
    private const int NUMBER_OF_FAKE_POSTS = 2500;

    public function run(): void
    {
        DB::connection()->disableQueryLog();

        Post::factory(self::NUMBER_OF_FAKE_POSTS)->create();
    }
}

One one row gets inserted, even if I replace the constant with an integer.

Passing the desired number with count doesn't work, either:

    {
        DB::connection()->disableQueryLog();

        Post::factory()
            ->count(self::NUMBER_OF_FAKE_POSTS)
            ->create();
    }
0 likes
3 replies
LaryAI's avatar
Level 58

The issue you're experiencing, where only one record is being inserted despite specifying a larger number, could be due to a few potential reasons. Let's go through some troubleshooting steps and solutions:

  1. Check Factory Relationships: Ensure that the subsite_id and user_id are being set correctly. If (new Subsite())->inRandomOrder()->first() or (new User())->inRandomOrder()->first() return null, it might cause issues. You should ensure that there are enough Subsite and User records in the database to satisfy the random selection.

  2. Use IDs Instead of Models: When assigning subsite_id and user_id, make sure you're assigning the ID, not the model instance. Modify your factory like this:

    public function definition(): array
    {
        $timestamp = $this->getFakeTimestamp();
    
        return [
            'title' => $this->faker->sentence(),
            'body' => $this->faker->paragraph(),
            'more_inside' => $this->faker->paragraph(),
            'subsite_id' => Subsite::inRandomOrder()->value('id'),
            'user_id' => User::inRandomOrder()->value('id'),
            'created_at' => $timestamp,
            'published_at' => $timestamp,
            'updated_at' => null,
            'deleted_at' => null,
            'is_published' => true,
            'state' => PostStateEnum::Published->value,
        ];
    }
    
  3. Check Database Constraints: Ensure there are no database constraints (like unique constraints) that might be preventing multiple records from being inserted. Check your database schema for any constraints that might be violated.

  4. Check for Errors: Run your seeder with error logging enabled to see if any errors are being thrown that might not be visible. You can enable query logging temporarily to see if there are any issues:

    DB::connection()->enableQueryLog();
    Post::factory(self::NUMBER_OF_FAKE_POSTS)->create();
    dd(DB::getQueryLog());
    
  5. Check Seeder Execution: Ensure that the seeder is being executed correctly. You can do this by adding a simple dd() statement in your run() method to confirm it's being called:

    public function run(): void
    {
        dd('Seeder is running');
        DB::connection()->disableQueryLog();
    
        Post::factory(self::NUMBER_OF_FAKE_POSTS)->create();
    }
    
  6. Database Transaction Issues: If you're using database transactions elsewhere in your code, ensure they are not affecting the seeder execution. Sometimes, if a transaction is not committed, it might appear as if the data is not being inserted.

By following these steps, you should be able to identify and resolve the issue with your seeder not inserting the expected number of records.

JussiMannisto's avatar

That's a seeder, not a migration. Are you actually running the seeder?

Please or to participate in this conversation.