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: Discovering attributes weight: 3

The find() method searches an entire class for usages of an attribute: on the class itself, all methods, properties, constants, and method parameters.

Basic usage

use Spatie\Attributes\Attributes;

#[Attribute(Attribute::TARGET_ALL)]
class Validate
{
    public function __construct(public string $rule = 'required') {}
}

#[Validate('exists:forms')]
class ContactForm
{
    #[Validate('string|max:255')]
    public string $name;

    #[Validate('email')]
    public string $email;

    public function submit(#[Validate('array')] array $data) {}
}

$results = Attributes::find(ContactForm::class, Validate::class);

count($results); // 4

The AttributeTarget object

Each result is an AttributeTarget instance with three properties:

use Spatie\Attributes\AttributeTarget;

foreach ($results as $result) {
    $result->attribute; // The instantiated attribute (e.g. Validate instance)
    $result->target;    // The Reflection object (ReflectionClass, ReflectionMethod, etc.)
    $result->name;      // A human-readable name (e.g. 'name', 'submit', 'submit.data')
}

For the example above, the results would be:

->name ->target type ->attribute->rule
ContactForm ReflectionClass exists:forms
name ReflectionProperty string|max:255
email ReflectionProperty email
submit.data ReflectionParameter array

For parameters, the name is formatted as method.parameter.

Getting attribute values as an array

Use toArray() to get all attribute properties as a keyed array. This is useful when you don't know the attribute's structure upfront:

foreach ($results as $result) {
    $result->toArray();
}

For the example above, this would return:

->name ->toArray()
ContactForm ['rule' => 'exists:forms']
name ['rule' => 'string|max:255']
email ['rule' => 'email']
submit.data ['rule' => 'array']

Using the reflection target

The target property gives you direct access to the underlying reflection object, so you can inspect additional details:

foreach (Attributes::find(ContactForm::class, Validate::class) as $result) {
    if ($result->target instanceof ReflectionProperty) {
        echo $result->target->getType(); // e.g. 'string'
    }
}

Finding all attributes

You can call find() without an attribute filter to get every attribute on a class:

$results = Attributes::find(ContactForm::class);

// Returns all attributes across the class, methods, properties, constants, and parameters

Inherited members

The find() method includes inherited methods, properties, and constants from parent classes. If a parent class has attributed members, they will appear in the results.

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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport