modeltyper icon indicating copy to clipboard operation
modeltyper copied to clipboard

Looses enum casted types if an accessor is defined.

Open itsemon245 opened this issue 11 months ago • 3 comments

When using only casts it generates the types perfectly. But when we try to define an accessor then it looses the type


namespace App\Models;

use App\Enums\PostType;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    
    protected $casts = [
        'type'=> PostType::class
    ];
    
    //Using an accessor makes the type to string
    protected function type(): Attribute
    {
        return Attribute::make(
            get: fn(string|PostType $value): string => $value instanceof PostType ? $value->value : $value,
        );
    }

}

itsemon245 avatar Feb 20 '25 08:02 itsemon245

I tried using a property annotation and custom mappings but it seem like the accessor takes priority over them

itsemon245 avatar Feb 20 '25 08:02 itsemon245

Ok I found this on the docs that lets me use my custom interface. but since modeltyper is not using the enum cast their is no types generated for that.

public array $interfaces = [
        'type' => [
            'type' => 'PostType',
        ],
    ];

But I don't see anything that generates enum types regardless if they are used in a cast or not

itsemon245 avatar Feb 20 '25 09:02 itsemon245

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Mar 23 '25 02:03 github-actions[bot]