Laravel Collection | Part 2

Welcome back to the exciting world of Laravel Collections! In Part 1, we explored some intriguing collection methods that help you work wonders with your data. Now, let's dive even deeper into the magic of Laravel Collections by uncovering another set of unique and lesser-known methods. These methods will empower you to handle data like a pro, making your code more concise and expressive than ever before.

16. dd()

The dd method stands for "Dump and Die." It allows you to inspect the contents of a collection at a specific point in your code and halt the execution, which is incredibly useful for debugging.

$collection = collect([1, 2, 3]);
$collection->dd();
// This will dump the collection and stop execution.

17. diff()

The diff method compares two collections and returns the values that are present in the first collection but not in the second.

$collection1 = collect([1, 2, 3]);
$collection2 = collect([2, 3, 4]);
$difference = $collection1->diff($collection2);
// Result: [1]

18. diffAssoc()

Similar to diff(), the diffAssoc method compares two collections based on both their keys and values, returning the differences.

$collection1 = collect(['a' => 1, 'b' => 2, 'c' => 3]);
$collection2 = collect(['b' => 2, 'c' => 3, 'd' => 4]);
$difference = $collection1->diffAssoc($collection2);
// Result: ['a' => 1]

19. diffAssocUsing()

The diffAssocUsing method allows you to specify a custom callback function to compare two collections based on both keys and values.

 
$collection1 = collect([1, 2, 3]);
$collection2 = collect([2, 3, 4]);
$difference = $collection1->diffAssocUsing($collection2, fn ($value1, $value2) => $value1 === $value2);
// Result: [1]

20. diffKeys()

The diffKeys method compares two collections based on their keys and returns the keys that are present in the first collection but not in the second.

 
$collection1 = collect(['a' => 1, 'b' => 2, 'c' => 3]);
$collection2 = collect(['b' => 2, 'c' => 3, 'd' => 4]);
$difference = $collection1->diffKeys($collection2);
// Result: ['a']

21. doesntContain()

The doesntContain method checks if the collection doesn't contain a specific value. It returns true if the value is not found.

 
$collection = collect(['apple', 'banana', 'cherry']);
$doesntContain = $collection->doesntContain('grape');
// Result: true

22. dot()

The dot method flattens a multi-dimensional collection into a single-dimensional one with dot-notation keys.

 
$collection = collect(['a' => ['b' => 1, 'c' => 2], 'd' => 3]);
$flattened = $collection->dot();
// Result: ['a.b' => 1, 'a.c' => 2, 'd' => 3]

23. dump()

Similar to dd, the dump method allows you to inspect the contents of a collection at a specific point in your code. However, it doesn't halt the execution.

$collection = collect([1, 2, 3]);
$collection->dump();
// This will dump the collection without stopping execution.

24. duplicates()

The duplicates method returns the duplicate values within a collection.

$collection = collect([1, 2, 2, 3, 4, 4]);
$duplicates = $collection->duplicates();
// Result: [2, 4]

25. duplicatesStrict()

The duplicatesStrict method returns the duplicate values within a collection using strict comparison (===).

$collection = collect([1, '1', 2, 2, 3]);
$duplicates = $collection->duplicatesStrict();
// Result: [2]

26. each()

The each method iterates over the collection and applies a callback function to each item.

$collection = collect([1, 2, 3]);
$collection->each(fn ($item) => echo $item * 2);
// Output: 246

27. eachSpread()

The eachSpread method is similar to each, but it applies a callback to items in pairs, assuming that the collection contains arrays of two items each.

$collection = collect([
    [1, 'apple'],
    [2, 'banana'],
]);
$collection->eachSpread(fn ($number, $fruit) => echo "Number: $number, Fruit: $fruit");

// Output: Number: 1, Fruit: appleNumber: 2, Fruit: banana

28. ensure()

The ensure method verifies that the items in the collection are of the specified type. It will throw an UnexpectedValueException if mismatched:

$collection->ensure(Payment::class);

$collection->ensure('int');

29. every()

The every method checks if all items in the collection satisfy a given truth-test.

$collection = collect([2, 4, 6, 8]);
$allEven = $collection->every(fn ($item) => $item % 2 === 0);
// Result: true

30. except()

The except method removes specified keys from the collection, returning a new collection without those keys.

$collection = collect(['a' => 1, 'b' => 2, 'c' => 3]);
$filtered = $collection->except(['b', 'c']);
// Result: ['a' => 1]

Congratulations! You've just scratched the surface of the vast capabilities of Laravel Collections. These methods are your secret weapons for handling data efficiently and elegantly in your Laravel projects. Stay tuned for more collection magic in the upcoming articles. Happy coding!