Add support for routes with nested model binding #725
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces support for defining routes with nested model bindings by specifying a
meta.parent
attribute in the controller's configuration. Theparent
attribute identifies the name of the related model.Example
Given the following draft:
Blueprint will generate a nested resource route instead of a standard resource route:
The
meta.parent
works not only for web or API resources, but for individual routes as well:The controller methods will automatically inject the parent model as a parameter:
And queries within the controller will utilize the parent model to scope operations. The relationship is inferred using the controller's name, following Laravel conventions:
Click here to see a full controller example
This PR also makes the necessary adjustments to the generated tests, both for Pest and PHPUnit, to fit the changes. You can check examples of these generated tests in
tests/fixtures/tests/pest/api-resource-nested.php
andtests/fixtures/tests/phpunit/api-resource-nested.php
.Testing
I have added tests to assert the correct generation of the route, controller, and tests when the
meta.parent
is present. I also added a test inControllerLexerTest
to assert that the parent is being set as expected.The previous tests are passing, ensuring that the introduction of this feature does not alter the existing functionality.
Let me know if this approach is valid and if you would like to see any changes. It would be great to have this feature in Blueprint. Thank you!