xiaohuilam/laravel-collection-transformable

Collection transformer setter. `collect()->setTransformer(WonderfulTransformer::class);`

v0.0.1 2019-06-05 09:20 UTC

This package is auto-updated.

Last update: 2024-12-05 22:47:17 UTC


README

Collection transformer setter. collect()->setTransformer(WonderfulTransformer::class);

Installation

composer require xiaohuilam/laravel-collection-transformable

Usage

define your transformer class:

namespace App\Http\Transformers;

class TestTransformer
{
    public function transform($item)
    {
        return [
            'id' => $item->id,
        ];
    }
}

then, set transformer as it.

$collection = collect([]);
$collection->setTransformer(App\Http\Transformers\TestTransformer::class);
dd($collection);

It outputs

Illuminate\Support\Collection {#796
  #items: array:2 [
    0 => array:1 [
      "id" => 1
    ]
    1 => array:1 [
      "id" => 2
    ]
  ]
}
>>>

License

MIT