8000 Looses enum casted types if an accessor is defined. · Issue #87 · fumeapp/modeltyper · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Looses enum casted types if an accessor is defined. #87
Open
@itsemon245

Description

@itsemon245

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,
        );
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0