PHPackages                             laravel-enso/dynamic-methods - 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. [Database &amp; ORM](/categories/database)
4. /
5. laravel-enso/dynamic-methods

ActiveLibrary[Database &amp; ORM](/categories/database)

laravel-enso/dynamic-methods
============================

Dynamic methods, relations or accessors for models

4.0.3(2mo ago)241.9k↓13.4%320MITPHPPHP ^8.0CI failing

Since Aug 27Pushed 2mo ago4 watchersCompare

[ Source](https://github.com/laravel-enso/dynamic-methods)[ Packagist](https://packagist.org/packages/laravel-enso/dynamic-methods)[ Docs](https://github.com/laravel-enso/dynamic-methods)[ RSS](/packages/laravel-enso-dynamic-methods/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (31)Used By (20)

Dynamic Methods
===============

[](#dynamic-methods)

[![License](https://camo.githubusercontent.com/62347ac6c51e8ce92e96401cdf4a96cf8cab29a73cd2d2e33848da883fbe295b/68747470733a2f2f706f7365722e707567782e6f72672f6c61726176656c2d656e736f2f64796e616d69632d6d6574686f64732f6c6963656e7365)](LICENSE)[![Stable](https://camo.githubusercontent.com/adb3fb5407a7e9b9e316bb127bbf6833526c6348917c09baf008d24a90ebf84e/68747470733a2f2f706f7365722e707567782e6f72672f6c61726176656c2d656e736f2f64796e616d69632d6d6574686f64732f76657273696f6e)](https://packagist.org/packages/laravel-enso/dynamic-methods)[![Downloads](https://camo.githubusercontent.com/f9f13174deb28c9db59d3cec6451bc709511279f10c2ccd7f14655cbc48ebc56/68747470733a2f2f706f7365722e707567782e6f72672f6c61726176656c2d656e736f2f64796e616d69632d6d6574686f64732f646f776e6c6f616473)](https://packagist.org/packages/laravel-enso/dynamic-methods)[![PHP](https://camo.githubusercontent.com/ef6afd4ccdaa708a9b1a0a353d6d03a13ca1f03887b8db701d4118dc30a6735a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e302532422d3737376262342e737667)](composer.json)[![Issues](https://camo.githubusercontent.com/aa6de453215ce547232d2fb56f19ac25269f02c008ada6f4aeea4a1bfdf3b707/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6c61726176656c2d656e736f2f64796e616d69632d6d6574686f64732e737667)](https://github.com/laravel-enso/dynamic-methods/issues)[![Merge Requests](https://camo.githubusercontent.com/2473a0815501a56ed644374e984ed078753ba11bdbcf9dce2785290d689fc8e5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722f6c61726176656c2d656e736f2f64796e616d69632d6d6574686f64732e737667)](https://github.com/laravel-enso/dynamic-methods/pulls)

Description
-----------

[](#description)

Dynamic Methods adds runtime-bound relations, instance methods, scopes, mutators, and static methods to Laravel models and classes.

The package scans `Dynamics` folders from configured vendor packages and from the host application, instantiates the dynamic definitions it finds, and binds their closures onto the target classes during boot.

In the Enso ecosystem it is the mechanism used to let independent packages augment shared models like `User` without editing those models directly.

Installation
------------

[](#installation)

Install the package:

```
composer require laravel-enso/dynamic-methods
```

The service provider is auto-registered and immediately binds all discovered dynamics on boot.

If you want to customize which vendor namespaces are scanned, publish the configuration:

```
php artisan vendor:publish --tag=dynamics-config
```

Default configuration:

```
return [
    'vendors' => ['laravel-enso'],
];
```

For application-level dynamics, place your classes under your app PSR-4 `Dynamics` folder. In a standard Laravel application this means `app/Dynamics`.

Features
--------

[](#features)

- Scans configured vendor packages for `Dynamics` classes.
- Scans the host application for its own `Dynamics` classes.
- Binds dynamic Eloquent relations through `resolveRelationUsing()`.
- Binds dynamic instance methods through `resolveMethodUsing()`.
- Supports dynamic scopes and attribute mutators through the `Abilities` trait.
- Supports dynamic static methods through `resolveStaticMethodUsing()`.
- Keeps the dynamic definition format small and package-friendly.

Usage
-----

[](#usage)

To receive dynamic instance methods, relations, scopes, and mutators, a model should implement `LaravelEnso\DynamicMethods\Contracts\DynamicMethods` and use `LaravelEnso\DynamicMethods\Traits\Abilities`.

Example model:

```
use Illuminate\Database\Eloquent\Model;
use LaravelEnso\DynamicMethods\Contracts\DynamicMethods;
use LaravelEnso\DynamicMethods\Traits\Abilities;

class User extends Model implements DynamicMethods
{
    use Abilities;
}
```

Define a dynamic relation in a package or app `Dynamics` class:

```
use Closure;
use LaravelEnso\ActionLogger\Models\ActionLog;
use LaravelEnso\DynamicMethods\Contracts\Relation;
use LaravelEnso\Users\Models\User;

class ActionLogs implements Relation
{
    public function bindTo(): array
    {
        return [User::class];
    }

    public function name(): string
    {
        return 'actionLogs';
    }

    public function closure(): Closure
    {
        return fn (User $user) => $user->hasMany(ActionLog::class);
    }
}
```

Define a dynamic instance method:

```
use Closure;
use Illuminate\Support\Facades\Session;
use LaravelEnso\DynamicMethods\Contracts\Method;
use LaravelEnso\Users\Models\User;

class IsImpersonating implements Method
{
    public function bindTo(): array
    {
        return [User::class];
    }

    public function name(): string
    {
        return 'isImpersonating';
    }

    public function closure(): Closure
    {
        return fn () => Session::has('impersonating');
    }
}
```

After boot, the bound methods can be used as if they were defined on the model itself:

```
$user->actionLogs();
$user->isImpersonating();
```

::: warning Note The binder discovers dynamics by reading package `composer.json` PSR-4 configuration and then scanning a `Dynamics` directory relative to that namespace root.

In practice, the package also relies on `laravel-enso/helpers` for `JsonReader`, even though that dependency is currently not declared in `composer.json`. :::

API
---

[](#api)

### Configuration

[](#configuration)

`config/dynamics.php`

Keys:

- `vendors`

The binder scans:

- `vendor//*`
- the application base path and its PSR-4 root

### Service Provider

[](#service-provider)

`LaravelEnso\DynamicMethods\AppServiceProvider`

Responsibilities:

- merges `enso.dynamics` config
- publishes `config/dynamics.php`
- runs the binder during boot

### Contracts

[](#contracts)

- `LaravelEnso\DynamicMethods\Contracts\Method`
- `LaravelEnso\DynamicMethods\Contracts\Relation`
- `LaravelEnso\DynamicMethods\Contracts\StaticMethod`
- `LaravelEnso\DynamicMethods\Contracts\DynamicMethods`
- `LaravelEnso\DynamicMethods\Contracts\DynamicStaticMethods`

Definition contracts require:

- `name(): string`
- `closure(): Closure`
- `bindTo(): array`

### Traits

[](#traits)

- `LaravelEnso\DynamicMethods\Traits\Methods`
- `LaravelEnso\DynamicMethods\Traits\StaticMethods`
- `LaravelEnso\DynamicMethods\Traits\Abilities`

`Abilities` extends `Methods` and also makes dynamic scopes, accessors, and mutators discoverable through Eloquent's standard model checks.

### Services

[](#services)

- `LaravelEnso\DynamicMethods\Services\Binder`
- `LaravelEnso\DynamicMethods\Services\Dynamics`
- `LaravelEnso\DynamicMethods\Services\Method`
- `LaravelEnso\DynamicMethods\Services\Relation`
- `LaravelEnso\DynamicMethods\Services\StaticMethod`

Depends On
----------

[](#depends-on)

Required Enso packages:

- [`laravel-enso/helpers`](https://docs.laravel-enso.com/backend/helpers.html) [↗](https://github.com/laravel-enso/helpers)

Framework dependency:

- [`laravel/framework`](https://github.com/laravel/framework) [↗](https://github.com/laravel/framework)

Contributions
-------------

[](#contributions)

are welcome. Pull requests are great, but issues are good too.

Thank you to all the people who already contributed to Enso!

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance86

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community32

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~110 days

Recently: every ~16 days

Total

23

Last Release

74d ago

Major Versions

1.1.4 → 2.0.02020-06-23

2.1.1 → 3.0.02024-01-09

3.2.0 → 4.0.02026-04-09

PHP version history (3 changes)1.0.0PHP &gt;=7.1.0

2.0.3PHP &gt;=8.0

3.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16073274?v=4)[Adrian Ocneanu](/maintainers/aocneanu)[@aocneanu](https://github.com/aocneanu)

---

Top Contributors

[![aocneanu](https://avatars.githubusercontent.com/u/16073274?v=4)](https://github.com/aocneanu "aocneanu (22 commits)")[![vmcvlad](https://avatars.githubusercontent.com/u/37445394?v=4)](https://github.com/vmcvlad "vmcvlad (10 commits)")[![gandesc](https://avatars.githubusercontent.com/u/14071925?v=4)](https://github.com/gandesc "gandesc (8 commits)")[![GITmanuela](https://avatars.githubusercontent.com/u/44998004?v=4)](https://github.com/GITmanuela "GITmanuela (2 commits)")[![raftx24](https://avatars.githubusercontent.com/u/10864136?v=4)](https://github.com/raftx24 "raftx24 (2 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (2 commits)")[![AbdullahiAbdulkabir](https://avatars.githubusercontent.com/u/33360580?v=4)](https://github.com/AbdullahiAbdulkabir "AbdullahiAbdulkabir (1 commits)")

---

Tags

dynamicdynamic-mutatorsdynamic-relationsdynamic-scopesensolaravellaravel-ensomethodsmodellaravel-ensodynamic-methods

### Embed Badge

![Health badge](/badges/laravel-enso-dynamic-methods/health.svg)

```
[![Health](https://phpackages.com/badges/laravel-enso-dynamic-methods/health.svg)](https://phpackages.com/packages/laravel-enso-dynamic-methods)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

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