Skip to content

Conversation

phpsa
Copy link
Owner

@phpsa phpsa commented Jan 27, 2022

This method allows us to create a nested child API controller route

  • artisan make:api:controller ChildController --parent=Parent
  • Confirm both the following methods work for relations in the ChildController
    • protected string $parentModel = Viewing::class;. -- this is where the relation and the url key will be the strtolower of the base classname, ie /api/viewing/{viewing}/notes and in the Notes model the method to get the related record is named viewing
      • should either of the 2 names in the first method be different, then an array can be passed:
        ['relationnameInModel' => 'routeParamName']
    • confirm index method gets only the records related to the specific parent
    • confirm create method auto-adds the id to the created record
    • Show / Update/ Delete should validate based on parent / child

@phpsa phpsa requested a review from samatcd January 27, 2022 22:23
@samatcd
Copy link
Collaborator

samatcd commented Jan 27, 2022

This is awesome.

Do we have to create a separate controller for each relationship we want to expose in this manner? Could we not simply have a list of models on the parent controller as a basic implementation option (while still support this Parent controller method for more complex situations)?

Just trying to find a way to reduce boilerplate for the 90% use-case where there will be little / no customisation to the controller.

@phpsa
Copy link
Owner Author

phpsa commented Jan 28, 2022

This is awesome.

Do we have to create a separate controller for each relationship we want to expose in this manner? Could we not simply have a list of models on the parent controller as a basic implementation option (while still support this Parent controller method for more complex situations)?

Just trying to find a way to reduce boilerplate for the 90% use-case where there will be little / no customisation to the controller.

@samatcd - Looked into this,
while a boilerplate option looks easy - it removes some of the finer bits that is inherited from Laravel directly - specifically in the case of the method parameter injections.

@phpsa phpsa closed this Jan 28, 2022
@phpsa phpsa reopened this Jan 28, 2022
@phpsa phpsa merged commit f4cc11d into 3.x Jan 28, 2022
@phpsa phpsa deleted the nested-api-controller branch January 28, 2022 03:02
phpsa pushed a commit that referenced this pull request Aug 6, 2022
…d-for-query-functions' into 'master'

Resolve "User model and namespace in policy stub is hardcoded for query functions"

Closes #105

See merge request composer/laravel-api-controller!118
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