Define Model Attributes With Laravel Fluent
Published on by Paul Redmond
Laravel Fluent is a package by Boris Lepikhin that provides an expressive way to define model attributes. This package automatically builds casts at runtime and gives you native auto-completion of model properties:
// Before /** * @property Collection $features * @property float $price * @property int $available */class Product extends Model{ protected $casts = [ 'features' => 'collection', 'price' => 'float', 'available' => 'integer', ];} // Afteruse Based\Fluent\Fluent; class Product extends Model{ use Fluent; public Collection $features; public float $price; public int $available;}
This package supports all native PHP types and laravel primitive casts:
class Order extends Model{ use Fluent; public int $amount; public Carbon $expires_at; #[AsDecimal(2)] public float $total; #[Cast('encrypted:array')] public array $payload;}
This package can also handle model relations. You can learn more about this package, get full installation instructions, and view the source code on GitHub.