1$collection = collect([1, 2, 3, 4]);
2
3$filtered = $collection->filter(function ($value, $key) {
4 return $value > 2;
5});
6
7$filtered->all();
8
9// [3, 4]
1/*
2 * The merge method merges the given array or collection with the original collection.
3 * If a string key in the given items matches a string key in the original collection,
4 * the given items's value will overwrite the value in the original collection:
5 */
6$collection = collect(['product_id' => 1, 'price' => 100]);
7$merged = $collection->merge(['price' => 200, 'discount' => false]);
8$merged->all(); // ['product_id' => 1, 'price' => 200, 'discount' => false]
9
10// If the given items's keys are numeric, the values will be appended to the end of the collection:
11$collection = collect(['Desk', 'Chair']);
12$merged = $collection->merge(['Bookcase', 'Door']);
13$merged->all(); // ['Desk', 'Chair', 'Bookcase', 'Door']
1$collection = collect(['product_id' => 1, 'price' => 100]);
2
3$merged = $collection->merge(['price' => 200, 'discount' => false]);
4
5$merged->all();
6
7// ['product_id' => 1, 'price' => 200, 'discount' => false]