PHPackages                             homesheer/laravel-assembler - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. homesheer/laravel-assembler

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

homesheer/laravel-assembler
===========================

eloquent model or DTO assembler for Laravel

V2.0.4(6y ago)329MITPHPPHP &gt;=7.1CI failing

Since Feb 13Pushed 6y ago1 watchersCompare

[ Source](https://github.com/homesheer/laravel-assembler)[ Packagist](https://packagist.org/packages/homesheer/laravel-assembler)[ Docs](http://i-mars.com/laravel-assembler)[ RSS](/packages/homesheer-laravel-assembler/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (2)Versions (11)Used By (0)

Laravel Assembler
=================

[](#laravel-assembler)

[中文说明](https://github.com/homesheer/laravel-assembler/blob/master/README_CN.md)

[![For Laravel 5](https://camo.githubusercontent.com/170e1265e3c21a9e5511d70afb176fade80c4b63f8b29cd5d5bc109adcc1a3a7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d352e2a2d677265656e2e737667)](https://github.com/laravel/laravel)[![For Lumen 5](https://camo.githubusercontent.com/18833b6e86cb7e2abb4a1991622488e5e344e3cfcf47c78f2e63d638ddbb58f3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c756d656e2d352e2a2d677265656e2e737667)](https://github.com/laravel/lumen)[![Latest Version on Packagist](https://camo.githubusercontent.com/d6193c8c884848cbe81684fd5f160bab333dbb1c1e52ec43249c1af277d18770/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686f6d6573686565722f6c61726176656c2d617373656d626c65722e737667)](https://packagist.org/packages/homesheer/laravel-assembler)[![Total Downloads](https://camo.githubusercontent.com/8868c7d42cf5ce7eee42fb5465a61c06a8b98b81e7cfc96983d1761c57000c0e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f6d6573686565722f6c61726176656c2d617373656d626c65722e737667)](https://packagist.org/packages/homesheer/laravel-assembler)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

Introduction
------------

[](#introduction)

Laravel Assembler is used to retrieve specifying fields of `Eloquent Model` or `DTO` for front-end requests, reducing the need for API interface upgrades.

Requirements
------------

[](#requirements)

This package requires Laravel 5.4 or newer.

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

[](#installation)

You can install the package via Composer:

```
composer require homesheer/laravel-assembler
```

You can publish the config file with:

```
php artisan vendor:publish --provider="HomeSheer\LaravelAssembler\AssemblerServiceProvider" --tag="config"
```

For Laravel 5.4 or older:

```
// config/app.php
HomeSheer\LaravelAssembler\AssemblerServiceProvider::class,
```

For Lumen:

```
// bootstrap/app.php
$app->register(HomeSheer\LaravelAssembler\AssemblerServiceProvider::class);
```

Usage
-----

[](#usage)

### 1. Instantiate `Assembler` or a subclass inherited from `Assembler` and pass in `Eloquent` model or `DTO`:

[](#1-instantiate-assembler-or-a-subclass-inherited-from-assembler-and-pass-in-eloquent-model-or-dto)

```
$assembler = new Assembler(User::find($id));
```

### 2. Call the `getAssembledData` method of `Assembler` to obtain the assembled data:

[](#2-call-the-getassembleddata-method-of-assembler-to-obtain-the-assembled-data)

```
$assembledUserData = $assembler->getAssembledData();
```

### 3. Add the query string `fields` to the URL and specify the fields to be acquired:

[](#3-add-the-query-string-fields-to-the-url-and-specify-the-fields-to-be-acquired)

The `controller` method of request returns the `$assembledUserData` of the previous step.

```
return $this->response($assembledUserData);
```

Request API interface to specify fields in the `fields` query string

```
// Get name and gender field information for users with ID 1
Http://localhost/users/1?fields={name,gender}

```

### 4. Attributes that do not exist in `Eloquent Model` or `DTO` can also be obtained:

[](#4-attributes-that-do-not-exist-in-eloquent-model-or-dto-can-also-be-obtained)

Simply create a new class that inherits from `Assembler` and defines the method of getting the virtual attributes

```
class UserAssembler extends Assembler
{
    public function getVirtualField()
    {
        return 'VirtualField';
    }
}
```

Then add the virtual field to the request

```
http://localhost/users/1?fields={name,gender,virtualField}

```

### 5. Supports three types of source data: objects with `getter` methods, objects without `getter` methods but with `public` attributes, and associative arrays:

[](#5-supports-three-types-of-source-data-objects-with-getter-methods-objects-without-getter-methods-but-with-public-attributes-and-associative-arrays)

Objects with `getter` methods:

```
class User
{
    public function getName()
    {
        return 'Mars';
    }
}
```

Objects without `getter` methods but with `public` attributes:

```
class User
{
    public $name = 'Mars';
}
```

Association array:

```
$user = [
    'name' => 'Mars',
];
```

### 6. Priority:

[](#6-priority)

The acquired field is retrieved from high to low in the following order until the field terminates and returns `null` by default if it is not retrieved.

- Customize the `getter` method in `Assembler`
- The `getter` method in `Eloquent Model` or `DTO`
- Attributes in `Eloquent Model` or `DTO`

### 7. Nested Cascade:

[](#7-nested-cascade)

In the case of `Eloquent Model`, natural support is provided for acquiring associated model attributes, as well as customized `getter` methods or attributes to return objects or arrays.

```
// Address is the associated object of user model
http://localhost/users/1?fields={name,gender,address{province,city}}

```

In the case of `DTO`, you need to define your own `getter` method or attribute that returns objects or arrays.

If it's an associative array, it's just a multidimensional associative array.

For response speed, it is not recommended to nest more than five layers.

### 8. config

[](#8-config)

If a customized `Assembler` and `maps` in `config/assembler.php` configure the corresponding relationship, the corresponding `getter` method of `getAssembledData` is automatically obtained when the `getAssembler` method is called after instantiating the parent class `Assembler`.

Contributing
------------

[](#contributing)

Contributions are welcome, [thanks to y'all](https://github.com/homesheer/laravel-assembler/graphs/contributors) :)

License
-------

[](#license)

Laravel Assembler is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Recently: every ~90 days

Total

10

Last Release

2258d ago

Major Versions

v1.0.5 → V2.0.02019-03-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/4ce359ceb53b6b9beb34ef7329836a1ca58b0e13f67cadb6139d7c28edabf9e8?d=identicon)[homesheer](/maintainers/homesheer)

---

Top Contributors

[![homesheer](https://avatars.githubusercontent.com/u/8615621?v=4)](https://github.com/homesheer "homesheer (1 commits)")

---

Tags

modeldtoassembler

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/homesheer-laravel-assembler/health.svg)

```
[![Health](https://phpackages.com/badges/homesheer-laravel-assembler/health.svg)](https://phpackages.com/packages/homesheer-laravel-assembler)
```

###  Alternatives

[mpociot/reanimate

Undo Laravel soft deletes

1221.2k](/packages/mpociot-reanimate)

PHPackages © 2026

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