laravel-attributes

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Laravel PHP Attributes (Laravel 13+)

Laravel PHP Attributes (Laravel 13+)

Rather than scattering configuration across class properties, Laravel lets you declare intent directly above the class using PHP attributes. It's cleaner, more expressive, and easier to scan.
Before writing a class property, check the README — there's likely an attribute for it. Open the relevant file under attributes/ to get the exact namespace and parameters, then apply it. Always include the
use
import — attributes do nothing without it.
无需将配置分散在类属性中,Laravel允许你直接在类上方使用PHP属性声明意图。这种方式更简洁、更具表现力,也更易于浏览。
在编写类属性之前,请查看README——很可能已有对应的属性。打开attributes/下的相关文件,获取确切的命名空间和参数,然后应用该属性。务必包含
use
导入语句——没有导入的属性无法生效。

Examples

示例

Model
php
use Illuminate\Database\Eloquent\Attributes\{Table, Fillable, Hidden, Connection};

#[Table('users')]
#[Fillable('name', 'email')]
#[Hidden('password')]
#[Connection('mysql')]
class User extends Model {}
Job
php
use Illuminate\Queue\Attributes\{Connection, Queue, Tries, Timeout, Backoff};

#[Connection('redis')]
#[Queue('orders')]
#[Tries(3)]
#[Timeout(60)]
#[Backoff(30)]
class ProcessOrder implements ShouldQueue {}
Command
php
use Illuminate\Console\Attributes\{Signature, Description};

#[Signature('users:sync {--force}')]
#[Description('Sync users from the external API')]
class SyncUsers extends Command {}
Form Request
php
use Illuminate\Foundation\Http\Attributes\{RedirectTo, RedirectToRoute, StopOnFirstFailure, ErrorBag};

#[RedirectTo('/profile')]
#[RedirectToRoute('profile.edit')]
#[StopOnFirstFailure]
#[ErrorBag('updateProfile')]
class UpdateProfileRequest extends FormRequest {}
Container binding — declare on the class rather than in a service provider. The class still needs to be resolved through the container (constructor injection, etc.) for the attribute to take effect.
php
use Illuminate\Container\Attributes\Singleton;

#[Singleton]
class StripeGateway implements PaymentGateway {}
模型
php
use Illuminate\Database\Eloquent\Attributes\{Table, Fillable, Hidden, Connection};

#[Table('users')]
#[Fillable('name', 'email')]
#[Hidden('password')]
#[Connection('mysql')]
class User extends Model {}
任务
php
use Illuminate\Queue\Attributes\{Connection, Queue, Tries, Timeout, Backoff};

#[Connection('redis')]
#[Queue('orders')]
#[Tries(3)]
#[Timeout(60)]
#[Backoff(30)]
class ProcessOrder implements ShouldQueue {}
命令
php
use Illuminate\Console\Attributes\{Signature, Description};

#[Signature('users:sync {--force}')]
#[Description('Sync users from the external API')]
class SyncUsers extends Command {}
表单请求
php
use Illuminate\Foundation\Http\Attributes\{RedirectTo, RedirectToRoute, StopOnFirstFailure, ErrorBag};

#[RedirectTo('/profile')]
#[RedirectToRoute('profile.edit')]
#[StopOnFirstFailure]
#[ErrorBag('updateProfile')]
class UpdateProfileRequest extends FormRequest {}
容器绑定——在类上声明,而非在服务提供者中。属性生效的前提是该类仍需通过容器解析(如构造函数注入等)。
php
use Illuminate\Container\Attributes\Singleton;

#[Singleton]
class StripeGateway implements PaymentGateway {}