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

bvfi-dev's avatar

Simple Laravel Factory issue

Im new to Laravel and am trying to grasp the seeding and the factory. I basically want to have main categories which will have null value as the parent_id field, and that will be the indicator that they are the main categories, then I want to have sub categories, which will have the id of the main categories as their parent_id. However I get thrown Maximum retries of 10000 reached without finding a unique value:

public function definition(): array
    {
        Category::factory(10)->create(['parent_id' => null, 'name' => $this->faker->unique()->jobTitle])
            ->each(function ($category){
                Category::factory(rand(0, 5))->create(['parent_id' => $category->id, 'name' => $this->faker->unique()->jobTitle]);
            });
        return [];
    }

I am not sure what causes this error as I create 10 Categories and then I create 1-5 sub-categories. In total I can have a maximum of 60 entries if it creates 5 sub categories for each category. I have also removed the ->unique() from the subcategories, so that only 10 Categories are unique, but to no avail.

EDIT: When I remove the unique from both I get the error: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) This is a new application and I have only 3 Models: Category, Listing and User, Im not sure whats triggering this, as my memory_limit is set to the max setting in Plesk -> 128M

0 likes
4 replies
tisuchi's avatar

@bvfi-dev How about this?

public function definition(): array
{
   Category::factory(10)->create(['parent_id' => null, 'name' => $this->faker->jobTitle])
       ->each(function ($category){
           Category::factory(rand(0, 5))->create(['parent_id' => $category->id, 'name' => $this->faker->jobTitle]);
       });
   return [];
}

I remove the unique() method, this will allow the jobTitle to be the same for main categories and sub-categories.

1 like
bvfi-dev's avatar

@tisuchi Well, that would work, I guess, but thats not the point and a PHP fatal error is thrown, I need my main categories to be unique. When I remove the unique it triggers the EDIT part in my post basically. Plesk offers memory_limit up to 128M and I have worked for over 3 years in cakePHP and never had a need to increase it, with over 5 thousand unique users and a pretty hefty database. So I need to figure out how to fix the code, without increasing the memory limit. Im surprised the limit is exceeded with such a small number of entries

1 like
tisuchi's avatar
tisuchi
Best Answer
Level 70

@bvfi-dev How about use unique(true)?

public function definition(): array
{
   Category::factory(10)->create(['parent_id' => null, 'name' => $this->faker->unique(true)->jobTitle])
       ->each(function ($category){
           Category::factory(rand(0, 5))->create(['parent_id' => $category->id, 'name' => $this->faker->unique(true)->jobTitle]);
       });
   return [];
}
2 likes
bvfi-dev's avatar

@tisuchi That doesnt throw the error and would be a solution, however I still get the PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) Im not sure why this is triggered as I created a new blank application without logic, only Models and Seeders to test this out and it still throws it. Im looking into ways I can maybe insert pauses or something, as its only barely 50 entries.

1 like

Please or to participate in this conversation.