Download the PHP package hongxunpan/eloquent-projection without Composer

On this page you can find all versions of the php package hongxunpan/eloquent-projection. 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 eloquent-projection

Eloquent Projection

中文文档

hongxunpan/eloquent-projection provides a focused projection layer for Eloquent read models. It is intended for scenarios where output shaping has become stable, repetitive, and worth standardizing across services.

Typical use cases include:

This package intentionally stays narrow in scope. It is not intended for:

If Eloquent's natural select, with, and toArray() flow already expresses the response cleanly, prefer the native approach over introducing projections.

Representative Business Scenarios

Administrative lists with conditional field exposure

A common scenario is an administrative list that returns a primary resource together with related identity data, while sensitive fields must be exposed differently depending on permissions. For example:

In this case, applyRelation() allows the service layer to preserve the original relation tree while applying a consistent projection profile to the related model.

Detail responses with stable serialization rules

Some detail endpoints need more than Eloquent's default serialization, but still do not justify introducing a dedicated DTO layer. A typical example is an alumni profile, submission record, or activity detail view that requires:

projectModel() is intended for this class of detail response: precise output shaping without forcing the service layer to hand-assemble arrays field by field.

Nested relationship projection in read pipelines

Read pipelines often traverse multiple relation levels, such as signup.alumniCard or record.user.profile. When output shaping rules need to be applied only to the nested relation, it is usually undesirable to flatten or rebuild the entire root response structure.

applyRelation() supports nested relation paths so that projection can stay local to the relevant read model while the root payload remains natural.

Array-oriented candidate and matching workflows

Some business flows consume arrays rather than mutable model instances. Examples include:

For these scenarios, projectList() provides a consistent way to convert a model list into projected arrays without duplicating serialization logic in each service.

Problems This Package Solves

Without a dedicated projection layer, the same read-model shaping logic tends to be reimplemented repeatedly across services. This package is designed to eliminate several recurring problems:

1. Repeated append + hidden + override code

Service methods often accumulate nearly identical code paths that clone models, append accessors, hide raw fields, convert to arrays, and conditionally overwrite output fields. Repetition makes behavior drift likely and increases the cost of maintenance.

2. Projection logic mixed into business services

When output shaping rules live directly inside service classes, the service layer starts handling both business decisions and serialization mechanics. That makes the core business flow harder to read, review, and evolve.

3. Inconsistent handling between model, list, and relation outputs

It is common for a rule to be implemented one way for a detail endpoint, another way for a list endpoint, and a third way for a related model inside another response. A reusable projection kernel keeps these paths aligned.

4. Weak separation between reusable kernel and scenario-specific policy

Most projects need two different layers:

This package provides the kernel, while leaving scenario-specific policy in the business codebase.

5. High regression risk when serialization rules evolve

When masking or exposure rules change, copy-pasted serialization logic forces updates across multiple services and relation paths. Centralized projection behavior reduces regression risk and makes rule changes easier to audit.

Before / After: Reducing Repetitive Service-Layer Code

Before

Without this package, service methods often end up mixing business flow and serialization mechanics:

Typical problems in the “before” style:

After

With hongxunpan/eloquent-projection, the same intent can be expressed declaratively:

What improves in the “after” style:

Installation

Compatibility

Current target compatibility:

Core Concepts

FieldOverrideRule

Represents a single field override rule.

ProjectionProfile

A declarative configuration object that contains projection rules only.

ProjectionProfile is intentionally configuration-only and does not execute projection logic.

ProjectionApplier

Executes a ProjectionProfile against models, collections, or relationships.

Error Handling

The package keeps its public entry classes at the root namespace:

At the same time, it exposes a lightweight package-level exception boundary under:

Current exception behavior:

all raise InvalidProjectionArgumentException.

This class extends PHP's native InvalidArgumentException, so consumers may either catch the native SPL type or the package-specific exception boundary, depending on how narrowly they want to scope error handling.

Parameter Reference

ProjectionProfile(array $append = [], array $hidden = [], array $overrides = [])

Parameter Role in the package Effect on output
$append Declares accessor or derived fields that should be appended before serialization The listed fields are included in serialized output
$hidden Declares fields that should be hidden before serialization The listed fields are removed from serialized output
$overrides Provides a list of field override rules May extend append/hidden behavior and may overwrite the final exposed value of target fields

FieldOverrideRule(string $targetField, string $sourceField, bool $enabled = true, bool $appendTargetField = false, bool $hideSourceField = false)

Parameter Role in the package Effect on source / target fields
$targetField Final field name exposed to consumers Receives the output value after projection
$sourceField Original attribute used as the value source Supplies the raw attribute value used for override
$enabled Controls whether source-to-target value override is executed true: target value is replaced with source value; false: target keeps its existing serialized value
$appendTargetField Automatically appends the target field during projection setup Ensures the target field participates in serialization
$hideSourceField Automatically hides the source field during projection setup Keeps the source field out of serialized output while still allowing it to be used internally

Current behavior note: $enabled controls only the value override step. appendTargetField and hideSourceField are resolved during projection setup regardless of whether $enabled is true or false.

Method Parameters

Method parameter Meaning
$model A single Eloquent model to mutate or project
$list An iterable collection of Eloquent models
$target A model or iterable that already owns the loaded relation to be projected
$relationName A relation path such as alumniCard or signup.alumniCard
$profile The projection rule set applied by ProjectionApplier

How Field Processing Works

The package applies projection in the following order:

  1. Start with the profile-level append and hidden definitions.
  2. Merge appendTargetField and hideSourceField contributions from each override rule.
  3. Apply append() and makeHidden() before serialization.
  4. For projectModel() / projectList(), serialize into arrays; for applyModel() / applyList() / applyRelation(), keep working on model instances.
  5. For each rule whose $enabled is true, copy the value from sourceField into targetField.

This means a single projection can both:

Example Parameter Breakdown

For the following rule:

The parameters mean:

So the final output behavior is:

API Overview

Method Behavior Typical use case
applyModel(Model $model, ProjectionProfile $profile): void Mutates the given model instance in place The same model instance continues through the read pipeline
applyList(iterable $list, ProjectionProfile $profile): void Mutates a list of models in place Paginated or aggregated model lists
applyRelation(Model\|iterable $target, string $relationName, ProjectionProfile $profile): void Applies a profile to a loaded relationship path Relationship-level output shaping without rebuilding the root payload
projectModel(Model $model, ProjectionProfile $profile): array Returns a projected array without requiring the caller to keep using the mutated output state of the same instance Detail responses or controlled one-off serialization
projectList(iterable $list, ProjectionProfile $profile): array Returns an array list of projected models Candidate lists or array-oriented downstream consumers

Usage Examples

Apply a projection to a loaded relationship

Project a single model into an array

Project a list into arrays

Design Boundaries

Keep this package focused on output projection for Eloquent read models.

Avoid using it for:

Recommended Integration Pattern

This package should remain the reusable kernel. Business projects should keep scenario-specific profiles in their own codebase, for example:

Prefer organizing profiles by the projected entity rather than by page name, modal name, or one-off endpoint context.

License

MIT


All versions of eloquent-projection with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0.20
illuminate/database Version ^9.52 || ^10.0 || ^11.0 || ^12.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 hongxunpan/eloquent-projection contains the following files

Loading the files please wait ...