Laravel 13 Attribute Guide: Transitioning from Properties to PHP Attributes

Laravel Daily////2 min read

Overview

Laravel introduces a significant shift in how developers configure classes by implementing 36 new PHP attributes. These attributes replace traditional protected properties—like $fillable or $hidden—with metadata directly above the class or method definition. This change aims to clean up class bodies and utilize native PHP 8 language features for better static analysis and cleaner syntax.

Prerequisites

To use these features, you should have a solid grasp of PHP attribute syntax (#[Attribute]). You should also be familiar with Laravel's Eloquent ORM, job dispatching, and Artisan command structures.

Key Libraries & Tools

  • Laravel: The latest version of the PHP framework.
  • Eloquent: The database mapper now supporting attribute-based model configuration.
  • Livewire: Often paired with these attributes in modern starter kits.
Laravel 13 Attribute Guide: Transitioning from Properties to PHP Attributes
New in Laravel 13: Added 36 (!) PHP Attributes

Code Walkthrough

Refactoring Eloquent Models

Previously, you defined model behavior using class properties. In the new version, you apply them as attributes:

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;

#[Fillable(['name', 'email'])]
#[Hidden(['password'])]
class User extends Model {}

Laravel reads these attributes at runtime, effectively populating the internal protected properties for you.

Enhancing Queued Jobs

For background tasks, you can now set retry limits and timeouts directly on the class:

use Illuminate\Queue\Attributes\Tries;
use Illuminate\Queue\Attributes\Timeout;

#[Tries(5)]
#[Timeout(60)]
class ProcessPodcast implements ShouldQueue {}

Syntax Notes

Attributes utilize the #[ ] syntax. Unlike properties, attributes can take constructor arguments, allowing for cleaner configuration of complex settings like ExponentialBackoff within a single line. This moves configuration logic out of the class body and into the header.

Practical Examples

You can now define Artisan command signatures and descriptions without declaring variables:

#[Signature('app:send-emails {user}')]
#[Description('Send a notification email to a specific user')]
class SendEmails extends Command {}

Tips & Gotchas

While Laravel now defaults to this style in its starter kits, these attributes are strictly optional. If you find multiple attributes on a single controller method hard to read, stick to the traditional $middleware arrays. Additionally, always ensure you import the correct namespace for each attribute to avoid "Attribute not found" errors.

Topic DensityMention share of the most discussed topics · 12 mentions across 6 distinct topics
Laravel
33%· products
Artisan
17%· products
Eloquent
17%· products
PHP
17%· products
Livewire
8%· products
Povilaskorop
8%· people
End of Article
Source video
Laravel 13 Attribute Guide: Transitioning from Properties to PHP Attributes

New in Laravel 13: Added 36 (!) PHP Attributes

Watch

Laravel Daily // 5:03

Tutorials, and demo projects with Laravel framework. Host: Povilas Korop

Who and what they mention most
Laravel
50.0%18
Filament
13.9%5
LiveWire
11.1%4
Composer
11.1%4
2 min read0%
2 min read