3.6 KiB
With Attributes (WithAttributes trait)
Scope
Use this reference when an action stores and validates input via internal attributes instead of method arguments.
Recap
- Documents attribute lifecycle APIs (
setRawAttributes,fill,fillFromRequest, readers/writers). - Clarifies behavior of key collisions (
fillFromRequest: request data wins over route params). - Lists validation/authorization hooks reused from controller validation pipeline.
- Includes end-to-end example from fill to
validateAttributes()andhandle(...).
Methods provided (WithAttributes trait)
setRawAttributes
Replaces all attributes with the provided payload.
$action->setRawAttributes([
'key' => 'value',
]);
fill
Merges provided attributes into existing attributes.
$action->fill([
'key' => 'value',
]);
fillFromRequest
Merges request input and route parameters into attributes. Request input has priority over route parameters when keys collide.
$action->fillFromRequest($request);
all
Returns all attributes.
$action->all();
only
Returns attributes matching the provided keys.
$action->only('title', 'body');
except
Returns attributes excluding the provided keys.
$action->except('body');
has
Returns whether an attribute exists for the given key.
$action->has('title');
get
Returns the attribute value by key, with optional default.
$action->get('title');
$action->get('title', 'Untitled');
set
Sets an attribute value by key.
$action->set('title', 'My blog post');
__get
Accesses attributes as object properties.
$action->title;
__set
Updates attributes as object properties.
$action->title = 'My blog post';
__isset
Checks attribute existence as object properties.
isset($action->title);
validateAttributes
Runs authorization and validation using action attributes and returns validated data.
$validatedData = $action->validateAttributes();
Methods used (AttributeValidator)
WithAttributes uses the same authorization/validation hooks as AsController:
prepareForValidationauthorizeruleswithValidatorafterValidatorgetValidatorgetValidationDatagetValidationMessagesgetValidationAttributesgetValidationRedirectgetValidationErrorBaggetValidationFailuregetAuthorizationFailure
Example
class CreateArticle
{
use AsAction;
use WithAttributes;
public function rules(): array
{
return [
'title' => ['required', 'string', 'min:8'],
'body' => ['required', 'string'],
];
}
public function handle(array $attributes): Article
{
return Article::create($attributes);
}
}
$action = CreateArticle::make()->fill([
'title' => 'My first post',
'body' => 'Hello world',
]);
$validated = $action->validateAttributes();
$article = $action->handle($validated);
Checklist
- Attribute keys are explicit and stable.
- Validation rules match expected attribute shape.
validateAttributes()is called before side effects when needed.- Validation/authorization hooks are tested in focused unit tests.
Common pitfalls
- Mixing attribute-based and argument-based flows inconsistently in the same action.
- Assuming route params override request input in
fillFromRequest(they do not). - Skipping
validateAttributes()when using external input.