fzaninotto / Faker

Faker is a PHP library that generates fake data for you
MIT License
26.8k stars 3.57k forks source link

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

Open wolfiton opened 4 years ago

wolfiton commented 4 years ago

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;