PHPackages                             kalshah/dynamic-relations-includes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. kalshah/dynamic-relations-includes

ActiveLibrary[API Development](/categories/api)

kalshah/dynamic-relations-includes
==================================

Dynamically include resources relations for API Requests

1.2.0(6y ago)282.5k2[1 issues](https://github.com/KhawlahElshah/dynamic-relations-includes/issues)MITPHPCI failing

Since Mar 26Pushed 6y ago2 watchersCompare

[ Source](https://github.com/KhawlahElshah/dynamic-relations-includes)[ Packagist](https://packagist.org/packages/kalshah/dynamic-relations-includes)[ RSS](/packages/kalshah-dynamic-relations-includes/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (5)Dependencies (5)Versions (6)Used By (0)

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
------------

[](#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: .

Usage
-----

[](#usage)

### Include relations based on request

[](#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

[](#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

[](#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 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

    ```
    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` or `www.example.com/profiles?include=socialMediaAccounts`

### Futher Explanations

[](#futher-explanations)

- Both `loadableRelations` and `loadableRelationsCount` 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
=======

[](#testing)

You can run the tests using

```
./vendor/bin/phpunit
```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 70.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

5

Last Release

2245d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e92304a8bf2520f5be557e2a05b5039fda24c4d5cd0349c01096dc3fbacf24d8?d=identicon)[KhawlahElshah](/maintainers/KhawlahElshah)

---

Top Contributors

[![KhawlahElshah](https://avatars.githubusercontent.com/u/31843646?v=4)](https://github.com/KhawlahElshah "KhawlahElshah (12 commits)")[![gentritabazi](https://avatars.githubusercontent.com/u/35135482?v=4)](https://github.com/gentritabazi "gentritabazi (5 commits)")

---

Tags

laravel relationshipsinclude relationsdynamically include relations

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kalshah-dynamic-relations-includes/health.svg)

```
[![Health](https://phpackages.com/badges/kalshah-dynamic-relations-includes/health.svg)](https://phpackages.com/packages/kalshah-dynamic-relations-includes)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.3k3](/packages/defstudio-telegraph)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)[jasara/php-amzn-selling-partner-api

A fluent interface for Amazon's Selling Partner API in PHP

1348.7k1](/packages/jasara-php-amzn-selling-partner-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
