The Blog

Laravel 5

Laravel 5

Laravel 5 Collections: Checking If a Collection Contains Items With isEmpty

John Koster
John Koster
April 22, 2018

The isEmpty method in Laravel's Collection class can be used to determine if a collection contains any items. If the collection is empty, true is returned; otherwise, false is returned. You can use this method to check if a collection has items before performing any operations on it.

Laravel 5

Laravel 5 Collections: Checking If a Collection Contains Items With isNotEmpty

John Koster
John Koster
April 22, 2018

The isNotEmpty method is used to determine whether a collection is not empty. This method returns a boolean value indicating whether the collection has any items or not. You can use this method to easily check if a collection contains any elements.

Laravel 5

Laravel 5 Collections: Combining Collection Elements Into a String With implode

John Koster
John Koster
April 22, 2018

The implode method in Laravel combines the items of a collection together, with the option to specify a "glue" between each item. Unlike PHP's implode function, Laravel's Collection allows for operating on arrays of primitive data types as well as arrays of objects and arrays. The returned value is a string with the specified glue between each item. Examples demonstrate how to use implode with both simple arrays and collections containing arrays or objects.

Laravel 5

Laravel 5 Collections: Combining the Keys of a Collection With the Values of Another Collection With combine

John Koster
John Koster
April 22, 2018

The combine method in Laravel is used to combine the keys of a collection with the values of another collection or array. It returns a new Collection instance without modifying the original collection. Both collections or arrays must have the same length, otherwise an error will be thrown. This method is similar to PHP's array_combine function.

Laravel 5

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

John Koster
John Koster
April 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

Laravel 5 Collections: Conditionally Executing a Callback On a Collection's Elements With unless

John Koster
John Koster
April 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

Laravel 5 Collections: Conditionally Executing a Callback On a Collection's Elements With when

John Koster
John Koster
April 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

Laravel 5 Collections: Conditionally Removing Elements From a Collection With reject

John Koster
John Koster
April 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

Laravel 5 Collections: Convert a Collection to a Native PHP Array With toArray

John Koster
John Koster
April 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

Laravel 5 Collections: Convert Collection Elements Into Object Instances With mapInto

John Koster
John Koster
April 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.

Latest posts

Where Things get Good: Moving on to Forte Development Phase 1

Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...

Read more
Forte Update: Backtracking, Metadata, HTML Validation, and More

A Forte development update: the parser now supports backtracking, improvements to node metadata, ide...

Read more
Parsing HTML and Blade Attributes in Forte

Wrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...

Read more
Switch Statements and Parser Extensions in Forte

Exploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...

Read more
Parsing Blade Comments in Forte

Digging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.

Read more
Thoughts on HTML Elements and Blade Components in Forte

This week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...

Read more