PHPackages                             hamidmp/dualresource - 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. hamidmp/dualresource

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

hamidmp/dualresource
====================

DualResource - Parameters mapper for both request and response

v1.0.1(4y ago)09MITPHPPHP ^7.4|^8.0

Since Mar 24Pushed 4y ago1 watchersCompare

[ Source](https://github.com/HamidMp/dualresource)[ Packagist](https://packagist.org/packages/hamidmp/dualresource)[ RSS](/packages/hamidmp-dualresource/feed)WikiDiscussions main Synced 6d ago

READMEChangelogDependenciesVersions (3)Used By (0)

DualResource (Laravel library)
------------------------------

[](#dualresource-laravel-library)

**DualResource** is a derived class of [Laravel Resource](https://laravel.com/docs/8.x/eloquent-resources).
By DualResource, in addition what [API Resource](https://laravel.com/docs/8.x/eloquent-resources) can to do, you can map request data to array with same template.

- Map Request Data To new array
- Map Model To json

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

[](#installation)

#### Require with [Composer](https://getcomposer.org/)

[](#require-with-composer)

```
composer require hamidmp/dualresource
```

Usage
-----

[](#usage)

```
class Person {
    // firstName, lastName, city, ...

    public function cars(){
        return $this->hasMany(Car::class);
    }
}

class Car {
    // person_id, name, model, ...
}
```

**1. Using *`fname`* and *`lname`* instead of *`firstName`* and *`lastName`* in client side (API or UI)**

```
class PersonDualResource extends DualResource  {

    protected function mapFields()
    {
        return [
            'fname'=>'fisrtName',
            'lname'=>'lastName',
        ];
    }

}
```

```
$data = PersonDualResource::fromRequest($request)->getParameters();
//$request contained: fname, lname
//$data will contain: firstName , lastName
```

*Note: Method `mapFields` will generate `toArray` method and you don't need to change it.*

**2. With relationship**

```
class PersonDualResource extends DualResource  {

    protected function mapFields()
    {
        return [
            'fname'=>'fisrtName',
            'lname'=>'lastName',
            'car_list'=>['cars',CarDualResource::class],
        ];
    }

}
```

```
$data = PersonDualResource::fromRequest($request)->getParameters();
//$request contained: fname, lname, car_list:[ {name, model},... ]
//$data will contain: firstName , lastName, cars=>[ [name,model],... ]
```

**3. Using anonymous function for mapping**

```
class PersonDualResource extends DualResource  {

    protected function mapFields()
    {
        return [
            'fname'=>'fisrtName',
            'lname'=>'lastName',

            'car_list'=>['cars',function($key){
                //for response
                return CarDualResource::collection($this->{$key});

            },function($data){
                //for request
                return CarDualResource::fromRequestData($data);

            }],
        ];
    }

}
```

*Note: You can ignore each function by returning `null` value: `return null;`*

**4. Using as normal Resource class (for response)**

```
$person=\App\Models\Person::with('cars')->find(1);
return \App\Http\Resources\PersonDualResource::fromModel($person)->response();
```

Methods
-------

[](#methods)

- Preparing class

```
//Declaring assign-map between model and data
//return array
protected function mapFields()
```

- from request data

```
//Preparing DualResource for mapping data from request
public static function fromRequest(Request $request)

//return array of mapped request data
public function getParameters($extra=[], $defaults=[])

//return array of mapped request data plus other ones
public function getAll($extra=[], $defaults=[])
```

- for response (json)

```
//return a new resource instance from one model
public static function fromModel(...$parameters)

//return a new anonymous resource collection from Models
public static function fromModels($resource)
```

License
-------

[](#license)

The DualSource library is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

Total

2

Last Release

1599d ago

PHP version history (2 changes)1.0.0PHP ^7.4

v1.0.1PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6995764?v=4)[hamidmp](/maintainers/hamidmp)[@HamidMp](https://github.com/HamidMp)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/hamidmp-dualresource/health.svg)

```
[![Health](https://phpackages.com/badges/hamidmp-dualresource/health.svg)](https://phpackages.com/packages/hamidmp-dualresource)
```

PHPackages © 2026

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