Lomkit / laravel-rest-api

Generate Api in seconds
https://laravel-rest-api.lomkit.com/
MIT License
444 stars 25 forks source link

Hide specific relationships or add option to remove not defined relationships inside the Resource #106

Closed Nowocyn closed 7 months ago

Nowocyn commented 7 months ago

Description

Hello,

we encountered an error that led back to the documentation. It isn't a bug from this package it was more a misunderstanding of how this package will handle relationships.

In the first implementation, we got only models and relationships that would be shipped with the model/API call. After some more work on the project, we got to the point where we had some models that would not be exposed to any api but had relationships with models that were exposed. Like a company that has several settings that don't affect any frontend/API but are a needed relationship. So company <-> settings. This Relation should not be exposed to the API yet.

It affects lines 76 to 116 in the file src/Http/Response.php it would be nice if there was an option to hide all relations that could not be mapped to a Rest Resource. The default behavior could be still that it would throw an exception that the given resource() is null on line 92 and with the option set to true the relation would be removed from it until it is defined in the Rest-Resource.

At the moment those lines work well with relations that return null and the collection will get { key: null } but not with not defined relationships inside the Resource which are defined in the model. Calling the delete route will trigger an exception with not missing defined relationships like that but it will deleted the entry successfully.

GautierDele commented 7 months ago

Hello @Nowocyn,

Could you give me an example to reproduce this ? I'm not quite sure what you mean, this might be kind of a bug if the package is misleading