laravel-enso/dynamic-methods

Dynamic methods, relations or accessors for models

Maintainers

Package info

github.com/laravel-enso/dynamic-methods

pkg:composer/laravel-enso/dynamic-methods

Statistics

Installs: 40 594

Dependents: 25

Suggesters: 0

Stars: 2

Open Issues: 0

4.0.3 2026-04-22 07:04 UTC

README

License Stable Downloads PHP Issues Merge Requests

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

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.dynamics config
  • publishes config/dynamics.php
  • runs the binder during boot

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

  • 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

  • LaravelEnso\DynamicMethods\Services\Binder
  • LaravelEnso\DynamicMethods\Services\Dynamics
  • LaravelEnso\DynamicMethods\Services\Method
  • LaravelEnso\DynamicMethods\Services\Relation
  • LaravelEnso\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!