Laravel 11에서 Model Casts 변경된거 알아보기

Updated on

laravel 11 에서 변경된 점이 하나 더 있는데,

Cast가 function 처럼 변경됬다는거다,

Model Casts Improvements
Model casts improvements were contributed by Nuno Maduro.

Laravel 11 supports defining your model's casts using a method instead of a property. This allows for streamlined, fluent cast definitions, especially when using casts with arguments:

https://laravel.com/docs/11.x/releases#model-cast-improvements

/**
 * Get the attributes that should be cast.
 *
 * @return array<string, string>
 */
protected function casts(): array
{
    return [
        'options' => AsCollection::using(OptionCollection::class),
                  // AsEncryptedCollection::using(OptionCollection::class),
                  // AsEnumArrayObject::using(OptionEnum::class),
                  // AsEnumCollection::using(OptionEnum::class),
    ];
}

그래서 이런식으로 하면 된다. collection, array, 등등 조금 더 직관적으로 Enum 을 사용하거나 Collection을 사용할 수 있다.