COLLECTIONS
Laravel
MARABESI
ARRAYS?
An array is a data structure that contains a
group of elements.
"
https://techterms.com/definition/array
<?php
$array = array(
'foo' => 'bar',
'bar' => 'foo',
);
// as of PHP 5.4
$array = [
'foo' => 'bar',
'bar' => 'foo',
];
?>
http://php.net/manual/en/language.types.array.php
The IlluminateSupportCollection class
provides a fluent, convenient wrapper for
working with arrays of data.
"
https://laravel.com/docs/5.0/collections
In computer science, a collection or
container is a grouping of some variable
number of data items (possibly zero) that
have some shared significance to the
problem being solved and need to be
operated upon together in some controlled
fashion.
"
https://en.wikipedia.org/wiki/Collection_(abstract_data_type)
$collection = collect([ 1, 2, 3 ]);
$collection = collect([ 'foo', 'bar' ]);
https://laravel.com/docs/collections
$collection = collect([ new User() ]);
$collection = collect([ 1, 2, 3 ]);
$collection = collect([ 'foo', 'bar' ]);
$collection = collect([ new User() ]);
https://laravel.com/docs/collections
$collection = collect([ 1, 2, 3 ]);
$collection = collect([ 'foo', 'bar' ]);
$collection = collect([ new User() ]);
https://laravel.com/docs/collections
$collection = collect([ 1, 2, 3 ]);
$collection = collect([ 'foo', 'bar' ]);
$collection = collect([ new User() ]);
https://laravel.com/docs/collections
$collection = collect([ 1, 2, 3 ]);
$collection = collect([ 'foo', 'bar' ]);
$collection = collect([ new User() ]);
https://laravel.com/docs/collections
use IlluminateSupportCollection;
$collection = new Collection();
COLLECTIONS
ARRAYS
X
IT SEEMS TO
BE THE
SAME THING...
$collection = collect([
'bar', 'foo'
]);
$array = [
'foo' => 'bar',
'bar' => 'foo',
];
HOW WOULD
YOU DO A SUM
FUNCTION?
<?php
$numbers = [
1,
2,
3
];
$total = 0;
for ($i = 0; $i < count($numbers); $i++) {
$total += $numbers[$i];
}
SUM
<?php
$numbers = [
1,
2,
3
];
$collection = collect($numbers);
$collection->sum();
SUM
HOW WOULD
YOU DO AN
AVG
FUNCTION?
<?php
$numbers = [
1,
2,
3
];
$total = 0;
for ($i = 0; $i < count($numbers); $i++) {
$total += $numbers[$i];
}
$total = $total / count($numbers);
AVG
<?php
$numbers = [
1,
2,
3
];
$collection = collect($numbers);
$collection->avg();
AVG
HOW WOULD
YOU DO A
SORT
FUNCTION?
<?php
$data = [
'c',
'd',
'a',
'b'
];
sort($data);
SORT
<?php
$data = [
'c',
'd',
'a',
'b'
];
$collection = collect($data);
$result = $collection->sort();
$values = $result->values()->all();
SORT
AVOID
NOTICES
<?php
$numbers = [
1,
2,
3
];
$numbers[4];
NOTICE
<?php
$numbers = [
1,
2,
3
];
$collection = collect($numbers);
$collection->get(4);
NOTICE
NULL
<?php
$numbers = [
1,
2,
3
];
$collection = collect($numbers);
$collection->get(4, 0);
NOTICE
0 (Zero)
<?php
$numbers = [
1,
2,
3
];
for ($i = 0; $i < count($numbers); $i++) {
$numbers[$i] = $numbers[$i] * 2;
}
MAP
<?php
$numbers = [
1,
2,
3
];
$collection = collect($numbers);
$result = $collection->map(function($item, $key) {
return $item * 2;
});
MAP
<?php
$numbers = [
1,
2,
3
];
for ($i = 0; $i < count($numbers); $i++) {
if ($numbers[$i] === 2) {
unset($numbers[$i]);
}
}
FILTER
<?php
$numbers = [
1,
2,
3
];
$collection = collect($numbers);
$result = $collection->filter(function($item, $key) {
return $item !== 2;
});
FILTER
!!! BONUS !!!
<?php
use IlluminateSupportStr;
Collection::macro('toUpper', function () {
return $this->map(function ($value) {
return Str::upper($value);
});
});
$collection = collect(['first', 'second']);
$upper = $collection->toUpper();
PERF TIME
<?php
$startTime = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
$data = [
'c',
'd',
'a',
'b',
];
sort($data);
}
echo 'Time: ' . number_format(( microtime(true) - $startTime), 4) . ' Seconds';
https://stackoverflow.com/questions/1200214/how-can-i-measure-the-speed-of-code-written-in-php
STEP PHP TIME
First run 7.1.7 2.4553
Second run 7.1.7 2.6595
Third run 7.1.7 2.4450
STEP PHP TIME
First run 7.2.0beta1 2.0294
Second run 7.2.0beta1 2.0281
Third run 7.2.0beta1 2.7551
<?php
require 'vendor/autoload.php';
use IlluminateSupportCollection;
$startTime = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
$data = [
'c',
'd',
'a',
'b'
];
$collection = collect($data);
$result = $collection->sort();
$result->values()->all();
}
echo 'Time: ' . number_format(( microtime(true) - $startTime), 4) . ' Seconds';
https://stackoverflow.com/questions/1200214/how-can-i-measure-the-speed-of-code-written-in-php
STEP PHP TIME
First run 7.1.7 11.7716
Second run 7.1.7 12.4548
Third run 7.1.7 12.2648
STEP PHP TIME
First run 7.2.0beta1 11.0426
Second run 7.2.0beta1 10.7730
Third run 7.2.0beta1 9.2139
WHY SHOULD I
USE
COLLECTIONS?
1. Eloquent models,
return collections by
default
2. Keep your code
readable
Avoid nested loops or unnecessary logic
3. Good
Documentation
LEARNING
MORE
https://adamwathan.me/refactoring-to-collections
https://leanpub.com/laravelcollectionsunraveled
https://github.com/illuminate/support/blob/master/Collection.php
MARABESI

Laravel collections an overview - Laravel SP