fumeapp / modeltyper

Generate TypeScript interfaces from Laravel Models
MIT License
135 stars 16 forks source link

I don't get any columns generated in my interfaces #56

Closed polsust closed 1 year ago

polsust commented 1 year ago

What happened?

After running :

php artisan model:typer

I noticed that none of the fillables of my models where outputed into the interfaces.

Laravel version: 9.51.0 php version: 8.1.19


I tried running:

composer require doctrine/dbal

and it returned:

curl error 60 while downloading https://repo.packagist.org/p2/doctrine/dbal~dev.json: SSL: no alternative certificate subject name matches target host
  name 'repo.packagist.org

Expected Behavior

I expected to recieve the generated interfaces with my model's fillables.

example of model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Key extends Model
{
    use HasFactory;

    protected $fillable = [
        "keyable_id",
        "keyable_type",
        "key_status_id",
        "key_type_id",
        "number",
        "description"
    ];

    public function keyable () {
        return $this->morphTo();
    }

    public function keyStatus () {
        return $this->belongsTo(KeyStatus::class);
    }

    public function keyType () {
        return $this->belongsTo(KeyType::class);
    }
}

example of autogenerated interface:

export interface Key {
  // relations
  keyable: Key;
  key_status: KeyStatus;
  key_type: KeyType;
}

Steps To Reproduce

  1. run
    composer require --dev fumeapp/modeltyper
    php artisan model:typer > types.d.ts
polsust commented 1 year ago

I managed to install doctrine/dbal but it didn't fix my problem

tcampbPPU commented 1 year ago

Are you using something like SQLite and these columns are not available yet? What happens when you run:

php artisan model:show Key
polsust commented 1 year ago

I am running mySQL

This is the output

 App\Models\Key ...................................................................................................................................
  Database ................................................................................................................................... mysql
  Table ....................................................................................................................................... keys

  Attributes ........................................................................................................................... type / cast

  Relations ........................................................................................................................................
  keyable MorphTo ................................................................................................................... App\Models\Key
  keyStatus BelongsTo ......................................................................................................... App\Models\KeyStatus
  keyType BelongsTo ............................................................................................................. App\Models\KeyType

  Observers ........................................................................................................................................
tcampbPPU commented 1 year ago

this package depends on the Laravel artisan model:show command, so if there is nothing generated from that command in that Attributes section, we cannot generate the type.

polsust commented 1 year ago

That is expected because the model doesn't have any Attributes but it does have columns in the database

tcampbPPU commented 1 year ago

Thats not what is meant int the Attributes section of that output, its not referring to Model Attributes (like castors and mutators). It will show both models columns and accessor, for example: image

What this packages does it takes whatever is in the Attributes section and turns into TS type. If nothing is there it would be expected nothing is generated from the model:typer command