kiwilan / typescriptable-laravel

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. Optional NPM package for Inertia.
https://packagist.org/packages/kiwilan/typescriptable-laravel
MIT License
27 stars 2 forks source link

[Bug]: DatabaseDriver becomes wrong driver #83

Open simonankele opened 2 weeks ago

simonankele commented 2 weeks ago

What happened?

I have three databases, which I have specified separately as connections in database.php. php artisan model:show specifies the database, which is the connection. This means that the database is unfortunately defined as a connection in the SchemaModel class $data['database'].

I have corrected this for myself as follows (SchemaModel):

$connection = config('database.connections.' . $data['database']);

        $self = new self(
            $schemaClass,
            $data['class'],
            $connection['driver'],
            $data['table'],
            $data['policy'] ?? null,
            [],
            [],
            $data['observers'] ?? [],
        );

How to reproduce the bug

  1. Creating second database
  2. Add additional connection 'second_connection' in the database.php for the new database
  3. Set $connection to 'second_connection' in the Model
    /**
     * The database connection that should be used by the model.
     *
     * @var string
     */
    protected $connection = 'second_connection';
  4. Run php artisan typescriptable
  5. Error because $databaseDriver 'second_connection' is not an Enum in the DatabaseDriverEnum

Package Version

3.1

PHP Version

8.3

Which operating systems does with happen with?

Linux

Notes

Sorry for bad bug report - it's my first ;-)

ewilan-riviere commented 2 weeks ago

I hadn't considered this possibility, I'll see how to manage this aspect, thanks for the feedback!

simonankele commented 2 weeks ago

Hello,

i have also empty types, when i have dynamic table names like:

    public function scopeSeason(Builder $builder, int $season): Builder
    {
        $this->setTable('Prefix'.$season);

        return $builder->setModel(model: $this);
    }

Perhaps, someone has a better solution to define dynamic tables.

ewilan-riviere commented 2 weeks ago

This is a very different problem from the first, so I'll see how to handle this one later. But yes, I had never thought of using a scope in this way.

I use show:model artisan command, so if Laravel can't see your dynamic table, this module can't see your dynamic table.

simonankele commented 2 weeks ago

This is a very different problem from the first, so I'll see how to handle this one later. But yes, I had never thought of using a scope in this way.

I use show:model artisan command, so if Laravel can't see your dynamic table, this module can't see your dynamic table.

Don't know how this should work, because the dynamic table name only takes effect when the scope method is called.