In Laravel 5

Laravel 5 Collections: Combining the Values of Multiple Traversable Sources With concat

Apr 22, 2018

The concat method in PHP creates a new collection by combining the values of the collection it is called on with a provided source. The source must implement the Traversable interface. The method does not modify the original collection instance and returns a new collection with the concatenated items. An example demonstrates how to use the concat method to concatenate two collections.
Laravel 5 Collections: Conditionally Executing a Callback On a Collection's Elements With unless

Apr 22, 2018

The unless method is the logical opposite of the when method in Laravel's collection class. It executes a provided callback on the collection only if the given value evaluates to false. Additionally, you can provide a default callback to be executed if the value is true. The unless method has the potential to modify the original collection instance.
Laravel 5 Collections: Conditionally Executing a Callback On a Collection's Elements With when

Apr 22, 2018

The when method in Laravel's collection class executes a callback on the collection if a provided boolean value evaluates to true. It also accepts an optional default callback if the value evaluates to false. Both callbacks receive the current collection instance as their argument. The method returns a reference to the original collection instance. In the example use, the when method is called twice. In the first call, the $callback callback is invoked, and in the second call, the $default callback is invoked. The resulting collection after the calls would contain two values: "Pushed the truth message!" and "Pushed the default message!".
Laravel 5 Collections: Conditionally Removing Elements From a Collection With reject

Apr 22, 2018

The reject method allows you to create a new Collection instance that excludes items that do not pass a given truth test. It takes a callback function as its only parameter, which determines whether each item should be included in the final collection. The reject method does not modify the original collection and returns a new collection with the filtered results. You can also use higher order messaging with reject to filter objects based on their properties or method results.
Laravel 5 Collections: Convert a Collection to a Native PHP Array With toArray

Apr 22, 2018

The toArray method in Laravel's Collection class allows you to convert the collection into a plain PHP array. It not only returns the underlying array used by the collection, but also converts any object instance that implements the "Illuminate\\Contracts\\Support\\Arrayable" interface into arrays. You can use this method to easily convert a collection into an array for further processing or manipulation.
Laravel 5 Collections: Convert Collection Elements Into Object Instances With mapInto

Apr 22, 2018

Learn how to use the mapInto method in Laravel to convert the contents of a collection to a new type. With mapInto, you can create a new instance of a specified class and supply each collection element's value and key as the arguments to the class's constructor. Take advantage of the Open Movie Database (OMDb) API to retrieve information about movie titles and use mapInto to convert a collection of movie titles into a Movie instance.
Laravel 5 Collections: Converting a Collection Into Something JSON Serializable With jsonSerialize

Apr 22, 2018

The jsonSerialize method in Laravel's Collection class allows you to convert the contents of a collection into a format that can be serialized using PHP's json_encode function. It follows specific rules for each element in the collection, including implementing the JsonSerializable interface, Jsonable contract, or Arrayable contract. If none of these rules are met, the element is returned as is. Here is an example demonstrating how to use jsonSerialize to get the serialized value of a collection.
Laravel 5 Collections: Converting a Collection to JSON With PHP's Magic __toString Method

Apr 22, 2018

Learn how to cast a Collection instance into a string in Laravel using the toJson method. When a collection is cast into a string, it returns its JSON representation. Find out how to achieve this and see an example in which a Collection with a single element is cast into a string, resulting in a JSON string representation.
Laravel 5 Collections: Converting a Collection's Contents to JSON With toJson

Apr 22, 2018

The toJson method allows you to convert the data stored in a collection instance into a JSON-encoded string. It internally makes a call to PHP's json_encode function, using any $options that were supplied. If a well-formatted value is desired, the JSON_PRETTY_PRINT constant can be passed as an option. However, unlike PHP's json_encode function, the toJson method does not provide a way to specify the depth of nested data structures. To convert a collection with a depth greater than the default limit of 512, you can use the json_encode function directly on the collection's jsonSerialize output.
Laravel 5 Collections: Converting a Multi-Dimensional Collection Into a Single Dimension With collapse

Apr 22, 2018

The collapse method in Laravel combines all the first-level items of a collection into a new, single collection. It does not recursively collapse inner arrays, but it can collapse both nested arrays and Collection instances. This method can be useful when you want to flatten a collection and work with a single-level array.
Laravel 5 Collections: Creating a Copy of a Collection With toBase

Apr 22, 2018

The toBase method in PHP returns a new collection instance that contains all the data from the current collection. This is useful when you want to modify the collection without changing the original instance. For example, you can create a new collection with uppercase names by using the toBase method and then applying the map function to modify the resulting collection.
Laravel 5 Collections: Creating Combination of Elements With crossJoin

Apr 22, 2018

The crossJoin method in PHP allows you to join the items within a collection with the values of input arrays or collections. This produces a Cartesian product, which can be useful for various applications. In the provided example, the method is used to determine the total number of product combinations that a manufacturer will be producing. This can be useful for production planning and distribution pipelines. The method is flexible and can handle more complex scenarios as well.
Laravel 5 Collections: Determine Which Collection Elements are Not Present in the Provided Collections With diff

Apr 22, 2018

The diff method in Laravel is used to find the items in a collection that are not present in another collection or an array. It returns a new instance of a collection.
Laravel 5 Collections: Determining if a Collection Contains an Element Using Strict Comparison With containsStrict

Apr 22, 2018

The containsStrict method in Laravel acts as a shortcut to the contains method with a strict comparison operator. It is used to check if a collection contains a given value using strict comparison. The method can be used with both key-value pairs or only the value.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.