krlove / eloquent-model-generator

Eloquent Model Generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Looking for an incorrectly-named pivot table

SturmB opened this issue · comments

In my current project, I have a table called armors and one called resources. This is a many-to-many relationship, so I create a joining/pivot table using the Laravel naming convention: armor_resource.

Unfortunately, eloquent-model-generator is expecting a non-standard naming scheme:

   Krlove\EloquentModelGenerator\Exception\GeneratorException

  Table armor_resources does not exist

  at vendor/krlove/eloquent-model-generator/src/Processor/TableNameProcessor.php:29
     25▕
     26▕         $schemaManager = $this->databaseManager->connection($config->getConnection())->getDoctrineSchemaManager();
     27▕         $prefixedTableName = Prefix::add($tableName);
     28▕         if (!$schemaManager->tablesExist($prefixedTableName)) {
  ➜  29▕             throw new GeneratorException(sprintf('Table %s does not exist', $prefixedTableName));
     30▕         }
     31▕
     32▕         $model
     33▕             ->setName(new ClassNameModel($className, EmgHelper::getShortClassName($baseClassName)))

      +15 vendor frames
  16  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

It is looking for armor_resources, which is not how the Laravel naming convention goes. Is this a bug? If so, while we wait for a fix, is there a way to tell the generator what the correct name of this pivot table is?

Hi @SturmB ,

Unfortunately there is no easy way for the library to recognise, that certain table serves the role of an intermediate table for Eloquent's Many-to-Many relation. I'd suggest that you specify the table name explicitly:

php artisan krlove:generate:model ArmorResource --table-name=armor_resource

I would imagine the same sort of logic that Laravel uses internally could be used here.

But thank you for the workaround of --table-name, that is helpful!