Download the PHP package kalshah/dynamic-relations-includes without Composer
On this page you can find all versions of the php package kalshah/dynamic-relations-includes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kalshah/dynamic-relations-includes
More information about kalshah/dynamic-relations-includes
Files in kalshah/dynamic-relations-includes
Package dynamic-relations-includes
Short Description Dynamically include resources relations for API Requests
License MIT
Informations about the package dynamic-relations-includes
Dynamic Relations Includes
This package allows you to include resources relationships and relationships count dynamically based on requests sent by sending query parameters with the request. This enables you to load relations only when you need them, which will boost your APIs performance.
Installation
This Package can be installed vie Composer
For Lumen is same procedure but you need to add helper request(). Here is code: https://github.com/albertcht/lumen-helpers/blob/master/src/helpers.php#L417.
Usage
Include relations based on request
For Example we have an API request which returns a list of posts and we want to include the comments of each post within the response.
To do this you first need to use the IncludeRelations
trait on you Post
model, and also you need to to add the comments
relation on the loadableRelations
array, so that the trait can load it for you.
and now you can send your requests where you need the comments, by adding the include
array parameter like so www.example.com/posts?include=comments
Include relations count based on request
For Example we have an API with requests which returns a list of posts and we want to include the comments count of each post within the response.
To do this you first need to use the IncludeRelations
trait on you Post
model, and also you need to to add the comments
relation on the loadableRelationsCount
array, so that the trait can load it for you.
and now you can send your requests where you need the comments count, by adding the include_count
array parameter like so www.example.com/posts?include_count=comments
You can use both include
and include_count
parameters as string or as an array
-
using it as a string with multiply includes
www.example.com/posts?include=comments,tags
- using it as an array with multiply includes
www.example.com/posts?include[]=comments&include[]=tags
Using different relationships types and conventions
-
you can include nested relationships, for example loading the comments and each comment creator
and requesting it using
www.example.com/posts?include=comments.creator
-
you can include and use camel cased relationships by adding it to the array like with its exact name
and then including it using either
www.example.com/profiles?include=social_media_accounts
orwww.example.com/profiles?include=socialMediaAccounts
Futher Explanations
- Both
loadableRelations
andloadableRelationsCount
arrays must be set on models which you want to to load their relations. - Both these array has been made to constrain which relations should be loaded, this to prevent dynamic loading of all relations which might hold sensitive data.
Testing
You can run the tests using
All versions of dynamic-relations-includes with dependencies
illuminate/support Version ~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0