A PHP library for representing and manipulating collections.
eslam-dev/collection is a PHP library for representing and manipulating collections.
Install this package as a dependency using Composer.
composer require eslam-dev/collection
the collect helper returns a new EslamDev\Collection instance for the given array. So, creating a collection is as simple as:
$collect = collect([
['id' => 1, 'name' => 'admin' , 'type' => 'admin'],
['id' => 2, 'name' => 'admin 2' , 'type' => 'admin'],
['id' => 3, 'name' => 'admin 3' , 'type' => 'admin'],
['id' => 4, 'name' => 'admin 4' , 'type' => 'admin'],
['id' => 5, 'name' => 'user 1' , 'type' => 'user' ],
['id' => 6, 'name' => 'user 2' , 'type' => 'user' ],
['id' => 7, 'name' => 'user 3' , 'type' => 'user' ],
]);
$collect->merge([
['id' => 8, 'name' => 'user 4', 'type' => 'user'],
['id' => 9, 'name' => 'user 5', 'type' => 'user'],
]);
$collect->add(['id' => 10, 'name' => 'user 6', 'type' => 'user']);
$collect->count();
$collect->where('type','user');
$collect->like('type','user');
# can like by array
$collect->like('type',['user','admin']);
$collect->whereIn('id',[1,2,3,4]);
$collect->whereNotIn('id',[5,3,7]);
$collect->orderBy('id','desc');
$collect->first();
$collect->all();
$collect->toArray();
$collect->toObject();
The eslam-dev/collection library is copyright © Eslam El Sherif and licensed for use under the terms of the MIT License (MIT).