Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Attribute Reader Laravel Package

spatie/php-attribute-reader

Lightweight PHP 8+ utility to read native attributes from classes, methods, properties, and parameters using reflection. Designed for simple, fast attribute discovery in frameworks and libraries, with an API that fits common annotation-style workflows.

View on GitHub
Deep Wiki
Context7

title: Reading class attributes weight: 1

All examples on this page use the following attribute and class:

#[Attribute]
class Description
{
    public function __construct(
        public string $text,
    ) {}
}

#[Description('A user account')]
class User {}

Getting an attribute

Use get() to retrieve a single attribute instance from a class. Returns null if the attribute is not present.

use Spatie\Attributes\Attributes;

$description = Attributes::get(User::class, Description::class);

$description->text; // 'A user account'

Checking for an attribute

Use has() to check if a class has a specific attribute:

Attributes::has(User::class, Description::class); // true
Attributes::has(User::class, SomeOtherAttribute::class); // false

Repeated attributes

If an attribute is marked as IS_REPEATABLE, use getAll() to retrieve all instances. Returns an empty array when no matching attributes exist.

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Tag
{
    public function __construct(public string $name) {}
}

#[Tag('featured')]
#[Tag('popular')]
class Post {}

$tags = Attributes::getAll(Post::class, Tag::class);

$tags[0]->name; // 'featured'
$tags[1]->name; // 'popular'

Without an attribute filter

All class-level methods also accept an optional attribute parameter. When omitted, they work with any attribute:

Attributes::get(User::class);    // first attribute, regardless of type
Attributes::has(User::class);    // true if the class has any attribute
Attributes::getAll(User::class); // all attributes on the class

Using object instances

All methods accept either a class string or an object instance:

$user = new User();

Attributes::get($user, Description::class);
Attributes::has($user, Description::class);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope