This package introduces a Cast attribute to enhance type casting for Eloquent model attributes. By leveraging PHP 8 attributes, this package allows you to define casts in a more modern, reusable, and maintainable way, especially when working with traits.
Note: This approach can be used together with the existing Eloquent casting system. The Cast attribute will be applied in addition to any other casts defined in the model.
Defining casts in Eloquent models can become repetitive, especially when the same casts are applied across multiple models. This package provides a clean and modern solution by allowing you to define casts using attributes, making it easier to reuse and maintain your code.
You can install the package via Composer:
composer require kontoulis/laravel-attribute-model-casting- PHP 8.0 or higher
- Laravel 11 or higher
You can use the Cast attribute to define type casting for model properties.
To resolve the actual casts, the model must use the included AttributeCasting trait.
Here's an example:
use Kontoulis\LaravelAttributeModelCasting\AttributeCasting;
use Kontoulis\LaravelAttributeModelCasting\Cast;
#[Cast('price', Price::class)]
trait HasPrice {
public function priceEquals(Price $other): bool
{
return $this->amount === $other->amount && $this->currency === $other->currency;
}
}
#[Cast('normalModelAttribute', 'int')]
class EloquentModelWithAttributeCasts extends Model
{
use AttributeCasting;
use HasPrice;
}In this example:
- The
HasPricetrait defines aCastattribute for thepriceproperty, allowing it to be reused across multiple models. - The
EloquentModelWithAttributeCastsclass uses theCastattribute to define a cast for thenormalModelAttributeproperty.
- Reusability: Define casts once in a trait and reuse them across multiple models.
- Modern Syntax: Leverage PHP 8 attributes for a cleaner and more expressive syntax.
- Reduced Redundancy: No need to manually define the same casts in every model.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This package is open-sourced software licensed under the MIT license.