Skip to content

eslam-dev/collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eslam-dev/collection

A PHP library for representing and manipulating collections.

About

eslam-dev/collection is a PHP library for representing and manipulating collections.

Installation

Install this package as a dependency using Composer.

composer require eslam-dev/collection

Usage

Generic 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' ],
]);

Methods

#merge

$collect->merge([
     ['id' => 8, 'name' => 'user 4', 'type' => 'user'],
     ['id' => 9, 'name' => 'user 5', 'type' => 'user'],
]);

#add

$collect->add(['id' => 10, 'name' => 'user 6', 'type' => 'user']);

#count

$collect->count();

#where

$collect->where('type','user');

#like

$collect->like('type','user');
# can like by array
$collect->like('type',['user','admin']);

#whereIn

$collect->whereIn('id',[1,2,3,4]);

#whereNotIn

$collect->whereNotIn('id',[5,3,7]);

#orderBy

$collect->orderBy('id','desc');

#first

$collect->first();

#all

$collect->all();

#toArray

$collect->toArray();

#toObject

$collect->toObject();

Copyright and License

The eslam-dev/collection library is copyright © Eslam El Sherif and licensed for use under the terms of the MIT License (MIT).