Download the PHP package mattsplat/dynamic-loading without Composer

On this page you can find all versions of the php package mattsplat/dynamic-loading. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package dynamic-loading

DynamicLoading

tests

Load relationships that were never defined on your models onto an existing Eloquent collection — in a single query — without foreign keys, relationship methods, or N+1 lookups.

dynamicLoad takes a collection you already have in memory, runs one sub‑query per model, stitches them together with UNION, executes them in one query per chunk of models (default 100), and attaches the results back to each model as a normal Eloquent relation you can access with $model->your_relation_name.

When the sub‑query conditions are the same for every model and there is a real linking column on the related table, use dynamicLoadWhereIn instead — it is a single whereIn query with no UNION.


Why

Eloquent's eager loading (with(), load()) requires a relationship method on the model and, usually, a foreign key. That is the right tool when the relationship is a stable part of your domain.

Sometimes you need something more ad‑hoc:

Defining a model method for each of these pollutes the model, and computing them per‑row is an N+1 query. dynamicLoad lets you express the relationship at the call site and still pay only one query.


Requirements

Package Version
PHP ^8.1 (Laravel 13 requires ^8.3)
illuminate/database ^10.0 \| ^11.0 \| ^12.0 \| ^13.0

Tested against Laravel 10, 11, 12 and 13. Laravel auto‑discovers the service provider; no manual registration is needed.

Install


API overview

The package registers two macros on Illuminate\Support\Collection (which Illuminate\Database\Eloquent\Collection extends), so they are available on any Eloquent result set.

dynamicLoad

Per‑model sub‑query, combined with UNION. Use when the sub‑query conditions vary per model.

Parameter Required Default Description
$relation_name yes — The name you will use to read the data back: $model->{$relation_name}. It only needs to exist here and at the point of access — no model method required.
$subQuery yes — fn ($model) => Builder. Called once per model in the collection. Return an unexecuted query builder (Model::query()..., not ->get()). Reference $model to scope the query to that record.
$relation_key no "{snake_model}_{model_key}" — e.g. user_id The column/alias used to group the unioned rows back to their model. If your sub‑query does not already select a column with this name, the package injects "{model_key value} as {relation_key}" for you. Must be a plain column identifier ([A-Za-z_][A-Za-z0-9_]*).
$model_key no the model's primary key name (getKeyName()) The attribute read from each model ($model->{$model_key}) to (a) bind into the sub‑query and (b) match returned rows back to the model.
$single no false false → the relation is set to an Eloquent collection (has‑many style). true → the relation is set to a single model or null (has‑one / latest‑of‑many style).
$chunkSize no 100 Models per UNION query. A collection larger than this is split into several queries. Pass 0 or null for a single UNION regardless of size.

Return value: the same collection of models, with the new relation set on every model:

$model->relationLoaded($relation_name) is therefore reliably true afterwards.

dynamicLoadWhereIn

A single whereIn query, no UNION. Use when every model needs the same sub‑query conditions and the related table has a real column pointing back at the model key.

$subQuery receives the collection of model‑key values and returns an unexecuted builder for the related model. The package adds ->whereIn($foreign_key, $keys) and groups the results by $foreign_key. The $single / no‑match semantics match dynamicLoad.

How dynamicLoad works

For a collection of 4 users and this call:

the package builds one query per chunk of (by default) 100 users:

executes it, groups the result by user_id (the $relation_key), and calls setRelation('r_ranks', ...) on each user with the rows whose user_id matches that user's id (the $model_key). The injected key is a bound parameter, not string‑interpolated SQL.

If the combined query fails, a MattSplat\DynamicLoading\Exceptions\DynamicLoadException is thrown (wrapping the underlying database exception).


Examples

1. Has‑many: ranks a user doesn't have

Schema

There is no foreign key from ranks back to users, and "ranks starting with r that aren't mine" is not a real relationship — but you can still load it.

Result shape

2. Latest‑of‑many: the single most recent login per user

Schema

A normal hasMany would pull every login row. Pass $single = true and a limit(1) sub‑query to get exactly one model per user in one query.

Result shape

Users with no logins get latest_login === null.

3. "Next" row by an ordered attribute

Uses another already‑loaded relation inside the sub‑query.

4. Custom keys

When the attribute to match on isn't the primary key, or the sub‑query already selects its own linking column, pass the keys explicitly.

If your sub‑query already selects a column named $relation_key, the package detects it and does not inject an alias, so you can drive the grouping with a real column:

5. Chaining

dynamicLoad returns the collection, so calls chain:

Each dynamicLoad here is one query per chunk of 100 users (2 queries total for a collection of ≤ 100).

6. dynamicLoadWhereIn: same condition for everyone

When the condition doesn't depend on the individual model and the related table has a real foreign key, skip the UNION entirely.

Schema

One query:

Add true as the 5th argument for the single‑model form:


Notes & caveats

Scaling

dynamicLoad issues one UNION query per $chunkSize models (default 100), so a 1,000‑model collection is 10 queries — still far better than the 1,000 of an N+1 loop, but not free. If your case fits dynamicLoadWhereIn (same conditions for all models, real linking column), prefer it: it is always a single query.

For true "top‑N rows per parent" at scale, a database LATERAL join (PostgreSQL, MySQL 8+) or Laravel's own hasOne()->ofMany() / subquery addSelect will outperform a wide UNION.

Database support

The package writes no dialect‑specific SQL — it only uses the query builder's union(), selectRaw() (with bound parameters) and whereIn(), which Laravel compiles for MySQL / MariaDB, PostgreSQL, SQLite and SQL Server. Notes:


Testing

The suite uses orchestra/testbench with an in‑memory SQLite database (tests/TestCase.php).


License

MIT


All versions of dynamic-loading with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/database Version ^10.0|^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mattsplat/dynamic-loading contains the following files

Loading the files please wait ...