kalshah / dynamic-relations-includes
Dynamically include resources relations for API Requests
Installs: 2 499
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- illuminate/http: ~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0
- illuminate/support: ~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0
Requires (Dev)
- illuminate/database: ~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0
- orchestra/testbench: ~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
This package is not auto-updated.
Last update: 2024-05-12 18:47:40 UTC
README
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
composer require kalshah/dynamic-relations-includes
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.
namespace App; use Illuminate\Database\Eloquent\Model; use Kalshah\DynamicRelationsInclude\IncludeRelations; class Post extends Model { use IncludeRelations; protected $loadableRelations = ['comments']; public function comments() { return $this->hasMany(Comment::class); } }
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.
namespace App; use Illuminate\Database\Eloquent\Model; use Kalshah\DynamicRelationsInclude\IncludeRelations; class Post extends Model { use IncludeRelations; protected $loadableRelationsCount = ['comments']; public function comments() { return $this->hasMany(Comment::class); } }
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
protected $loadableRelations = ['comments', 'comments.creator'];
and requesting it usingwww.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
namespace App; use Illuminate\Database\Eloquent\Model; use Kalshah\DynamicRelationsInclude\IncludeRelations; class Profile extends Model { use IncludeRelations; protected $loadableRelationsCount = ['socialMediaAccounts']; public function socialMediaAccounts() { return $this->hasMany(socialMediaAccount::class); } }
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
./vendor/bin/phpunit