Download the PHP package homesheer/laravel-assembler without Composer
On this page you can find all versions of the php package homesheer/laravel-assembler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download homesheer/laravel-assembler
More information about homesheer/laravel-assembler
Files in homesheer/laravel-assembler
Package laravel-assembler
Short Description eloquent model or DTO assembler for Laravel
License MIT
Homepage http://i-mars.com/laravel-assembler
Informations about the package laravel-assembler
Laravel Assembler
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
This package requires Laravel 5.4 or newer.
Installation
You can install the package via Composer:
You can publish the config file with:
For Laravel 5.4 or older:
For Lumen:
Usage
1. Instantiate Assembler
or a subclass inherited from Assembler
and pass in Eloquent
model or DTO
:
2. Call the getAssembledData
method of Assembler
to obtain the assembled data:
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.
Request API interface to specify fields in the fields
query string
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
Then add the virtual field to the request
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:
Objects without getter
methods but with public
attributes:
Association array:
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 inAssembler
- The
getter
method inEloquent Model
orDTO
- Attributes in
Eloquent Model
orDTO
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.
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
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
Contributions are welcome, thanks to y'all :)
License
Laravel Assembler is open-sourced software licensed under the MIT license.