Back

Seeding with same code another model throws error??


chimmel posted 5 days ago

I have two tables, manufacturers & models that I'm seeding via factories.

ManufacturersFactory:

use \Faker\Generator as Faker;

$factory->define(\Modules\Cars\Entities\Manufacturer::class, function (Faker $faker) {
    return [
        'title' => $faker->company,
        'summary' => $faker->paragraph,
        'url' => $faker->domainName,
        'dealer' => $faker->boolean(20),
    ];
});

ManufacturersTableSeeder:

namespace Modules\Cars\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class ManufacturersTableSeeder extends Seeder
{
    public function run()
    {
      factory(\Modules\Cars\Entities\Manufacturer::class, 25)->create();
    }
}

^ Seeds perfectly successfully.

CarsDatabaseSeeder:

namespace Modules\Cars\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class CarsDatabaseSeeder extends Seeder
{
    public function run()
    {
      \Eloquent::unguard();

      \DB::table('models')->truncate();
      \DB::table('manufacturers')->truncate();

      \DB::statement('SET FOREIGN_KEY_CHECKS=0;');

      $this->call([
        ManufacturersTableSeeder::class,
        ModelsTableSeeder::class
      ]);

      \DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
}

ModelsFactory:

use \Faker\Generator as Faker;

$factory->define(\Modules\Cars\Entities\Model::class, function (Faker $faker) {
    return [
      'title' => $faker->word,
      'manufacturer_id' => $faker->numberBetween(1,\Modules\Cars\Entities\Manufacturer::get()->count()),
      'length_ft' => $faker->numberBetween(30,160),
    ];
});

ModelsTableSeeder:

namespace Modules\Cars\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class ModelsTableSeeder extends Seeder
{
    public function run()
    {
        factory(\Modules\Cars\Entities\Model::class, 60)->create();
    }
}

No errors if I comment out the factory function.

I do not see how these two are different and thereby why I am receiving the following error when running with Models:

[email protected]:~/code$ php artisan module:seed Cars
Seeding: Modules\Cars\Database\Seeders\ManufacturersTableSeeder
Seeding: Modules\Cars\Database\Seeders\ModelsTableSeeder
PHP Fatal error:  Cannot declare class Modules\Cars\Entities\Model because the name is already in use in /home/vagrant/code/Modules/Cars/Entities/Model.php on line 7

   [Symfony\Component\Debug\Exception\FatalErrorException]
   Cannot declare class Modules\Cars\Entities\Model because the name is already in use

Help please???

Update: changed both manufacturers & models to CSV imports now, manufacturers works fine same way as this one but this one is still producing that same error unless I comment out the [next to] last line:

    $csv = Reader::createFromPath(__DIR__.'/data/models.csv', 'r');
    $csv->setHeaderOffset(0);
    $headers = $csv->getHeader();
    $records = $csv->getRecords();
    foreach ($records as $r) {
      $cols = array_combine($headers, $r);
      $i = [];
      foreach($headers as $h)
      {
        if(!\Schema::hasColumn('models',$h)) continue;
        if($h == 'manufacturer') {
          $cols[$h] = Manufacturer::where('title', $cols[$h])->first()->id;
          $i['manufacturer_id'] = $cols[$h];
        }
        else $i[$h] = $cols[$h];
      }
      \Modules\Cars\Entities\Model::create($i);
    }

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us