Laravel Community Tools by Tighten

Arrays on steroids

Submitted by anouarabdsslm - 11 years ago

Hi guys,
well let me share with you this trick that you may not know:
isn't going to be great to use collection methods with your own array . well laravel make this simple , we can know turn our arrays to collections and interact with its just as we do with eloquent collection .

$devs = [
['name' => 'Anouar Abdessalam','email' => 'dtekind@gmail.com'],
['name' => 'Bilal Ararou','email' => 'have@noIdea.com']
];

//Now I can overwrite the array variable:

$devs = new Illuminate\Support\Collection( $devs );

//Now I can do something like :
$devs->first() //['name' => 'Anouar Abdessalam','email' => 'dtekind@gmail.com']

$devs->last() //['name' => 'Bilal Ararou','email' => 'have@noIdea.com']
$devs->push(['name' => 'xroot','email' => 'xroot@root.com']); //this will add the new dev to the collection .