.cursor/skills/laravel-actions/references/with-attributes.md
WithAttributes trait)Use this reference when an action stores and validates input via internal attributes instead of method arguments.
setRawAttributes, fill, fillFromRequest, readers/writers).fillFromRequest: request data wins over route params).validateAttributes() and handle(...).WithAttributes trait)setRawAttributesReplaces all attributes with the provided payload.
$action->setRawAttributes([
'key' => 'value',
]);
fillMerges provided attributes into existing attributes.
$action->fill([
'key' => 'value',
]);
fillFromRequestMerges request input and route parameters into attributes. Request input has priority over route parameters when keys collide.
$action->fillFromRequest($request);
allReturns all attributes.
$action->all();
onlyReturns attributes matching the provided keys.
$action->only('title', 'body');
exceptReturns attributes excluding the provided keys.
$action->except('body');
hasReturns whether an attribute exists for the given key.
$action->has('title');
getReturns the attribute value by key, with optional default.
$action->get('title');
$action->get('title', 'Untitled');
setSets an attribute value by key.
$action->set('title', 'My blog post');
__getAccesses attributes as object properties.
$action->title;
__setUpdates attributes as object properties.
$action->title = 'My blog post';
__issetChecks attribute existence as object properties.
isset($action->title);
validateAttributesRuns authorization and validation using action attributes and returns validated data.
$validatedData = $action->validateAttributes();
AttributeValidator)WithAttributes uses the same authorization/validation hooks as AsController:
prepareForValidationauthorizeruleswithValidatorafterValidatorgetValidatorgetValidationDatagetValidationMessagesgetValidationAttributesgetValidationRedirectgetValidationErrorBaggetValidationFailuregetAuthorizationFailureclass 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);
validateAttributes() is called before side effects when needed.fillFromRequest (they do not).validateAttributes() when using external input.