xxxxxxxxxx
$collection = collect([
['product' => 'Desk', 'price' => 200],
['product' => 'Chair', 'price' => 100],
]);
$collection->contains('product', 'Bookcase');
// false
xxxxxxxxxx
$collection = collect([1,2,3,4]);
$collection->each(function($item){
return $item*$item;
});
// [1,4,9,16]
xxxxxxxxxx
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$value = $collection->get('name');
// taylor
xxxxxxxxxx
use Illuminate\Support\Collection;
$itemsNew = new Collection(['Apple', 'Ball', 'Cat', 'Dog']); //collection object class
dd($itemsNew);
// Or another way no need to use Collection class
$items = collect(['Apple', 'Ball', 'Cat', 'Dog', 'Bag']); //collection helper
//dd($items); //give collection
//dd($items->all()); //give collection as array
//dd($items->count()); //give count
dd($items->toArray()); //give collection as array format
xxxxxxxxxx
$collection = collect([1, 2, 3]);
$collection->when(true, function ($collection) {
return $collection->push(4);
});
$collection->all();
// [1, 2, 3, 4]
xxxxxxxxxx
$collection = collect([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(3);
$chunk->all();
// [0, 1, 2]
xxxxxxxxxx
$coll = new Collection();
$coll->name = 'name';
$coll->value = 'value';
$coll->description = 'description';
xxxxxxxxxx
all
average
avg
chunk
chunkWhile
collapse
collect
combine
concat
contains
containsOneItem
containsStrict
count
countBy
crossJoin
dd
diff
diffAssoc
diffAssocUsing
diffKeys
doesntContain
dot
dump
duplicates
duplicatesStrict
each
eachSpread
ensure
every
except
filter
first
firstOrFail
firstWhere
flatMap
flatten
flip
forget
forPage
get
groupBy
has
hasAny
implode
intersect
intersectAssoc
intersectByKeys
isEmpty
isNotEmpty
join
keyBy
keys
last
lazy
macro
make
map
mapInto
mapSpread
mapToGroups
mapWithKeys
max
median
merge
mergeRecursive
min
mode
nth
only
pad
partition
percentage
pipe
pipeInto
pipeThrough
pluck
pop
prepend
pull
push
put
random
range
reduce
reduceSpread
reject
replace
replaceRecursive
reverse
search
select
shift
shuffle
skip
skipUntil
skipWhile
slice
sliding
sole
some
sort
sortBy
sortByDesc
sortDesc
sortKeys
sortKeysDesc
sortKeysUsing
splice
split
splitIn
sum
take
takeUntil
takeWhile
tap
times
toArray
toJson
transform
undot
union
unique
uniqueStrict
unless
unlessEmpty
unlessNotEmpty
unwrap
value
values
when
whenEmpty
whenNotEmpty
where
whereStrict
whereBetween
whereIn
whereInStrict
whereInstanceOf
whereNotBetween
whereNotIn
whereNotInStrict
whereNotNull
whereNull
wrap
zip
xxxxxxxxxx
$items = collect([
['label' => 'cake', 'name' => 'Cake', 'price' => 150],
['label' => 'pizza', 'name' => 'Pizza', 'price' => 250],
['label' => 'puff', 'name' => 'Veg Puff', 'price' => 20],
['label' => 'samosa', 'name' => 'Samosa', 'price' => 14]
]);
//dd($items->sortBy('price'));
//dd($items->sum('price'));
dd($items->max('price'));