laravel-enso / dynamic-methods
Dynamic methods, relations or accessors for models
Requires
- php: ^8.0
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
- dev-master
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-upgrade/laravel13-core12
- dev-feature/laravel12
- dev-refactor/dynamic-relations
- dev-upgrade/enso6
- dev-feature/addsHasMethod
- dev-upgrade/laravel8
- dev-fixes/stylci
This package is auto-updated.
Last update: 2026-04-22 07:07:56 UTC
README
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
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
- Scans configured vendor packages for
Dynamicsclasses. - Scans the host application for its own
Dynamicsclasses. - Binds dynamic Eloquent relations through
resolveRelationUsing(). - Binds dynamic instance methods through
resolveMethodUsing(). - Supports dynamic scopes and attribute mutators through the
Abilitiestrait. - Supports dynamic static methods through
resolveStaticMethodUsing(). - Keeps the dynamic definition format small and package-friendly.
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
Configuration
config/dynamics.php
Keys:
vendors
The binder scans:
vendor/<configured-vendor>/*- the application base path and its PSR-4 root
Service Provider
LaravelEnso\DynamicMethods\AppServiceProvider
Responsibilities:
- merges
enso.dynamicsconfig - publishes
config/dynamics.php - runs the binder during boot
Contracts
LaravelEnso\DynamicMethods\Contracts\MethodLaravelEnso\DynamicMethods\Contracts\RelationLaravelEnso\DynamicMethods\Contracts\StaticMethodLaravelEnso\DynamicMethods\Contracts\DynamicMethodsLaravelEnso\DynamicMethods\Contracts\DynamicStaticMethods
Definition contracts require:
name(): stringclosure(): ClosurebindTo(): array
Traits
LaravelEnso\DynamicMethods\Traits\MethodsLaravelEnso\DynamicMethods\Traits\StaticMethodsLaravelEnso\DynamicMethods\Traits\Abilities
Abilities extends Methods and also makes dynamic scopes, accessors, and mutators discoverable through Eloquent's standard model checks.
Services
LaravelEnso\DynamicMethods\Services\BinderLaravelEnso\DynamicMethods\Services\DynamicsLaravelEnso\DynamicMethods\Services\MethodLaravelEnso\DynamicMethods\Services\RelationLaravelEnso\DynamicMethods\Services\StaticMethod
Depends On
Required Enso packages:
Framework dependency:
Contributions
are welcome. Pull requests are great, but issues are good too.
Thank you to all the people who already contributed to Enso!