PHP Attributes in Laravel 13: goodbye to sparse properties in models
Il problema delle property sparse nei modelli Eloquent Fino a Laravel 12, configurare un modello Eloquent significava definire proprietà come $table , $fillable , $hidden , $casts e $primaryKey sparse nel corpo della classe. Con modelli complessi — quelli con decine di campi, relazioni multiple e accessor personalizzati — queste dichiarazioni si mescolano a relazioni e metodi, rendendo il codice…
Laravel 13 introduces native PHP Attributes as an optional alternative to traditional properties for configuring Eloquent models. This change allows for a more compact and readable way of declaring model configurations, such as table names, fillable fields, and casts, using PHP 8.1+ syntax. The Attributes can be used in over 15 areas of the framework, including models, controllers, jobs, and notifications, and do not impact performance.
The old property syntax remains fully supported, and migration can be done incrementally. IDEs and static analysis tools, such as PhpStorm, VS Code, and PHPStan, already support Laravel 13 Attributes.
Written by urgent.news from Dev.to's report — not a translation of it. Machine-written — may contain errors; check the original before relying on it.