denysovvl / easy-collector
Collect your data easy
Installs: 92
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/denysovvl/easy-collector
Requires
- php: >=7.4
- ext-json: *
- laravel/framework: >=v8.0.0
This package is auto-updated.
Last update: 2025-12-29 03:40:53 UTC
README
EasyCollector is a package for Laravel for making collectors like Laravel Resources.
Installation
Use composer to install
composer require denysovvl/easy-collector
Creating Collector
Create Collector:
php artisan make:collector MyAwesomeCollector
This command will create Collector class in App/Collectors directory:
<?php namespace App\Collectors; use Denysovvl\EasyCollector\BaseCollector; class MyAwesomeCollector extends BaseCollector { /** * @param $element * @param mixed $args * @return array */ public function collect($element, $args) { return [ //TODO: define your data here ]; } }
Usage
Available methods
# returns array MyAwesomeCollector::toArray($data); # returns Collection MyAwesomeCollector::toCollection($data); # returns object MyAwesomeCollector::toObject($data);
Examples
There are few examples to use:
Define collect() method in your Collector class
public function collect($element, $args) { return [ 'some_key' => $element->name, 'another_key' => $element->age, 'role' => 'human', 'created_at' => now(), 'updated_at' => now() ]; }
Somewhere in your controllers or services:
public function myMethod() { $users = User::all(); $collector = MyAwesomeCollector::toArray($users); }
In $collector you will see something like this:
[
[
'some_key' => John Connor,
'another_key' => 35,
'role' => 'human',
'created_at' => '2020-10-10 10:10:00',
'updated_at' => '2020-10-10 10:10:00'
],
[
'some_key' => Sarah Connor,
'another_key' => 55,
'role' => 'human',
'created_at' => '2020-10-10 10:10:00',
'updated_at' => '2020-10-10 10:10:00'
],
...
]
Or you can use your arrays as parameters:
public function myMethod() { $users = [ [ 'name' => 'Lua', 'age' => 28, ... ], [ 'name' => 'Mark', 'age' => 70, ... ], ... ]; $collector = MyAwesomeCollector::toArray($users); }
Also, you can pass additional values
public function myMethod() { $users = User::all(); $role = 'human'; $collector = MyAwesomeCollector::toArray($users, compact('role')); }
These values will be available through $args parameter
public function collect($element, $args) { return [ 'some_key' => $element->name, 'another_key' => $element->age, 'role' => $args->role 'default_value' = $args->qwerty 'created_at' => now(), 'updated_at' => now() ]; }
If you use non-existent property such as $args->qwerty you will get null as value for default_value key,
but you can change this behavior with defaultArgsValue() method:
class MyAwesomeCollector extends BaseCollector { public function collect($element, $args) { return [ 'some_key' => $element->name, 'another_key' => $element->age, 'role' => $args->role 'default_value' = $args->qwerty 'created_at' => now(), 'updated_at' => now() ]; } public function defaultArgsValue() { return 'empty :('; } }
And you will get
[
[
'some_key' => John Connor,
'another_key' => 35,
'role' => 'human'
'default_value' => 'empty :(',
'created_at' => '2020-10-10 10:10:00',
'updated_at' => '2020-10-10 10:10:00'
],
[
'some_key' => Sarah Connor,
'another_key' => 55,
'role' => 'human',
'default_value' => 'empty :(',
'created_at' => '2020-10-10 10:10:00',
'updated_at' => '2020-10-10 10:10:00'
],
...
]
By the way, EasyCollector is a convenient way to mass insert to model
public function myMethod() { $users = User::all(); AnotherModel::insert(MyAwesomeCollector::toArray($users)); }