fzaninotto/Faker

Doc: Add distinct values to a category table in Laravel 7 using factories (no documentation for this common case)

wolfiton opened this issue · 0 comments

Summary

Trying to add distinct values to a category table in Laravel 7 using factories

Self-enclosed code snippet for reproduction

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Category;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

$categories = ['Hardware', 'Software', 'Planning', 'Tools'];

foreach ($categories as $categoryName) {
    factory(Category::class)->create([
        'name' => $categoryName,
        'slug' => Str::slug($categoryName),
    ]);
}
//Seeder
factory('App\Category', [detect categories array count])->create();

Expected output

id:1 name:Hardware'

Actual output

require(/home/dan/Codes/laravel/jobportal/database/factories/CategoryFactory.php): failed to open stream: Too many open files

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factory.php:220
    216|         $factory = $this;
    217| 
    218|         if (is_dir($path)) {
    219|             foreach (Finder::create()->files()->name('*.php')->in($path) as $file) {
  > 220|                 require $file->getRealPath();
    221|             }
    222|         }
    223| 
    224|         return $factory;