Skip to content

Looses enum casted types if an accessor is defined. #87

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

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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