Attributes everywhere: configuring models, jobs and controllers in Laravel 13
Laravel 13 lets you swap many protected properties for PHP attributes. A tour of the ones I reach for on models, queued jobs and controllers, with before-and-after code.
- Published
- Reading time
- 3 minutes
- Written for
- Laravel 13.x
One of the quieter themes of Laravel 13 is attributes. Configuration that used to live in protected properties can now sit on the class as a PHP attribute, right where you'd look for it.
Nothing here is mandatory. Properties still work, and you can mix both styles. But once you've read a model where the table, fillable fields and observers are all declared in three lines at the top, it's hard to go back.
Models
Here's a fairly typical model before:
class Flight extends Model
{
protected $table = 'my_flights';
protected $fillable = ['name'];
public $timestamps = false;
}And with attributes:
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
#[Table('my_flights', timestamps: false)]
#[Fillable(['name'])]
class Flight extends Model
{
//
}#[Table] takes named arguments for the other table-level settings too, like key, keyType, incrementing and dateFormat:
#[Table(key: 'uuid', keyType: 'string', incrementing: false)]
class Flight extends Model
{
//
}There are single-purpose attributes as well, if you prefer one idea per line: #[WithoutTimestamps], #[WithoutIncrementing], #[DateFormat('U')], #[Connection('mysql')] and #[Unguarded].
Scopes and observers
Global scopes and observers already had attributes in recent versions, and they fit right in:
use App\Models\Scopes\AncientScope;
use App\Observers\UserObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
#[ScopedBy([AncientScope::class])]
#[ObservedBy([UserObserver::class])]
class User extends Authenticatable
{
//
}Local scopes are a method with the #[Scope] attribute, no scope prefix needed:
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
#[Scope]
protected function popular(Builder $query): void
{
$query->where('votes', '>', 100);
}
// User::popular()->get();Queued jobs
Jobs are where attributes really shine, because retry behavior is the first thing you want to see when debugging a failing job.
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Attributes\FailOnTimeout;
use Illuminate\Queue\Attributes\MaxExceptions;
use Illuminate\Queue\Attributes\Timeout;
use Illuminate\Queue\Attributes\Tries;
#[Tries(25)]
#[MaxExceptions(3)]
#[Timeout(120)]
#[FailOnTimeout]
class ProcessPodcast implements ShouldQueue
{
use Queueable;
public function handle(): void
{
// ...
}
}Read top to bottom, that says: retry up to 25 times (useful when you release() the job while rate limited), but give up after 3 real exceptions; kill it after 120 seconds, and mark it failed rather than retrying when that happens.
If you'd rather retry for a period of time than a number of attempts, there's #[RetryFor]:
use Illuminate\Queue\Attributes\RetryFor;
#[RetryFor(hours: 1, minutes: 30)]
class SyncInventory implements ShouldQueue
{
use Queueable;
}For unique jobs, #[UniqueFor(3600)] replaces the $uniqueFor property. More on uniqueness in the queues post.
Controllers
Laravel 13 adds #[Middleware] and #[Authorize] for controllers, so the rules for an action sit right above it:
use App\Models\Comment;
use App\Models\Post;
use Illuminate\Routing\Attributes\Controllers\Authorize;
use Illuminate\Routing\Attributes\Controllers\Middleware;
#[Middleware('auth')]
class CommentController
{
#[Middleware('subscribed')]
#[Authorize('create', [Comment::class, 'post'])]
public function store(Post $post)
{
// ...
}
}The class-level middleware applies to every action. The method-level ones stack on top. #[Authorize] runs the create policy check with the route's post parameter before your method body runs.
Should you convert everything?
I wouldn't do a big-bang refactor. My rule: new classes use attributes; old classes get converted when I'm already editing them. If your team uses Rector, watch for rules that automate the conversion.
The one thing to agree on as a team is consistency within a class. A model with #[Fillable] and also a $fillable property is a code review comment waiting to happen.
Sources: the Laravel 13 docs for Eloquent, queues and controllers.