Closed n1c closed 10 months ago
Hey @n1c are you able to provide any additional examples? Maybe like your db schema or the column or the Laravel output from the model:show command?
From my initial testing I do not run into this same issue when I have something like a json
column:
[
"name" => "data",
"type" => "json",
"increments" => false,
"nullable" => false,
"default" => null,
"unique" => false,
"fillable" => true,
"hidden" => false,
"appended" => null,
"cast" => "object",
]
export interface Submission {
// columns
id: number
data: Record<string, unknown>
}
Thanks for the quick reply! Looks like yours did exactly what I was expecting :)
Is it maybe 'cause I'm using sqlite which treats $table->json('data')->nullable();
in a migration as text
?
Database ........................................... sqlite
Table .............................................. fields
Attributes .................................... type / cast
id increments, unique ....................... integer / int
route_id .......................................... integer
field_id nullable ................................. integer
key fillable ....................................... string
type fillable ................ string / App\Enums\FieldType
data nullable, fillable ..................... text / object
created_at nullable ................... datetime / datetime
updated_at nullable ................... datetime / datetime
deleted_at nullable .............................. datetime
Relations .................................................
field BelongsTo .......................... App\Models\Field
route BelongsTo .......................... App\Models\Route
Thanks for the quick reply! Looks like yours did exactly what I was expecting :)
np, thanks for the Laravel output this should be able to help me out!
using sqlite could cause some strange behavior yes, but if the Laravel model show command can show it, our command should be able to as well. So I can continue looking further into.
While I do that can you just verify you are using the latest version of this package v2.2.6
Also if you could send the full Exception thrown that would help
Yeah I'm on 2.2.6.
composer.lock:
"name": "fumeapp/modeltyper",
"version": "v2.2.6",
It doesn't throw an exception, just an error in the output:
$ php artisan model:typer --no-hidden --optional-relations
"Unknown cast type: object" // vendor/fumeapp/modeltyper/src/Actions/WriteColumnAttribute.php:86
export interface Field {
// columns
id: number
route_id: number
field_id: number|null
key: string
type: FieldType
data: unknown|null
created_at: string|null
updated_at: string|null
deleted_at: string|null
// relations
field?: Field
route?: Route
}```
What happened?
I have a field of type "object", which is a legal type according to the list here: https://laravel.com/docs/10.x/eloquent-mutators#attribute-casting but when running the command I get an unknown cast type error.
Expected Behavior
I'd expect something even as basic as
any|null
to be set as the type or maybeRecord<any, any>
Steps To Reproduce
Have a model with something like