Skip to content

[12.x] Add Request input contextual attributes #56217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from

Conversation

ziadoz
Copy link
Contributor

@ziadoz ziadoz commented Jul 4, 2025

This PR adds #[Query], #[Input], #[Post] and #[Validated] contextual attributes, similar to #[Route(...)], that allow GET or POST data to be retrieved from the current request.

They can be used to populate a data object from the request:

final readonly class SearchData
{
    public function __construct(
        #[Query('query')] public string $query,
        #[Query('order', 'asc')] public string $order,
    ) {}
}

Route::get('/', function (SearchData $data) {
    dump($data);
});

Populate a data object from the validated request:

final readonly class PersonData
{
    public function __construct(
        #[Validated('name')] public string $name,
        #[Validated('age', 24)] public int $age,
    ) {}
}

Route::post('/', function (StorePersonRequest $request, PersonData $data) {
    dump($data);
});

Pass a request field directly to the controller:

Route::post('/post/status', function (Post $post, #[Post('status')] $status) {
    $post->update(['status' => $status]);
});

Or the entire GET/POST data array to the controller:

Route::get('/', function (#[Post] array $post) {
    dump($post);
});

I had a skim through the various container methods, but I couldn't see anything that would let me accurately identify the validated request instance in the container, so I tweaked the service provider to store it in the container as request.validated after resolving.

Copy link

github-actions bot commented Jul 4, 2025

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@ziadoz ziadoz changed the title [12.x] Add Request Get/Post Contextual Attributes [12.x] Add Request input contextual attributes Jul 8, 2025
@ziadoz ziadoz marked this pull request as ready for review July 16, 2025 20:54
@taylorotwell
Copy link
Member

I kinda feel like a more comprehensive solution to request objects like spatie/laravel-data would be more appropriate.

@ziadoz ziadoz deleted the request-contextual-attributes branch July 18, 2025 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants