kiwilan / typescriptable-laravel

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. Optional NPM package for Inertia.

Home Page:https://packagist.org/packages/kiwilan/typescriptable-laravel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Missing model columns

ngekoding opened this issue · comments

What happened?

The generated types only include some table columns.

Let's say we have this users table columns:

Screen Shot 2024-01-10 at 16 45 31

User's model:

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    /**
     * Get the user's responses.
     */
    public function responses(): HasMany
    {
        return $this->hasMany(Response::class);
    }
}

And it is what we get:

Screen Shot 2024-01-10 at 16 46 53

How to reproduce the bug

  1. Just generate TS types for models
  2. Generated type only contains some columns

Package Version

^1.11.33

PHP Version

8.2.10

Which operating systems does with happen with?

macOS

Notes

No response

Strange bug, I will try to reproduce it, thanks 👍

@ngekoding I try to reproduce your problem, but I can't, can you provide a repository like this one?

https://github.com/ewilan-riviere/laravel-typescriptable-test

Well, I found the issue when using DB_PREFIX, when trying to remove it, the generated types looks good:

declare namespace App.Models {
  export interface User {
    id?: number
    name: string
    email: string
    role: string
    email_verified_at?: Date
    created_at?: Date
    updated_at?: Date
    password?: any
  }
}

So, maybe it is the problem with the prev issue: #44

Thanks, I will fix this soon!

Can you try latest release: 1.11.35?

Screen Shot 2024-01-12 at 06 02 36

It seems to have worked now.

Sorry maybe it is a different thing, but as for now I just thinking about the Date type like for created_at, how can we cast it to JavaScript Date as we get string from the server and passed directly as component props?

Like we have this page component:

// Pages/User/UserList.vue
<script setup lang="ts">
defineProps<{
  users: App.Models.User[]
}>
</script>

<template>
  <ul>
    <li v-for="user in users">The user <b>{{ user.name }}</b> was created on {{ user.created_at }}.</li>
  </ul>
</template>

And in the server we have:

$users = User::all();

return Inertia::render('User/UserList', [
   'users' => $users
]);

Thanks, I will fix this soon!

Can you try with latest release?

Sorry, will try it as soon as possible.

Solved!

Screen Shot 2024-01-16 at 08 13 24