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