The Blog

Laravel

Laravel

Laravel Collection Public API: toJson

John Koster
John Koster
November 30, 2016

The toJson method in Laravel allows you to easily convert a collection into its JSON representation. By default, it uses PHP's json_encode function and can accept optional $options for encoding. You can also format the JSON output by passing in the JSON_PRETTY_PRINT constant. If you have deeply nested data structures and need to specify a higher depth for encoding, you can accomplish this by using json_encode directly on the jsonSerialize method of the collection instance.

Laravel

Laravel Collection Public API: transform

John Koster
John Koster
November 30, 2016

The transform method in Laravel is similar to the map method, but instead of creating a new Collection instance, it modifies the original one. This method is useful when you want to make changes to each item in the collection. In the provided code example, the transform method is used to convert all the strings in the collection to uppercase.

Laravel

Laravel Collection Public API: unique

John Koster
John Koster
November 30, 2016

The unique method in Laravel allows you to get all the unique items in a collection. By default, it performs a case-sensitive check, but you can provide a $key option to further restrict which items are returned. The method returns a new instance of Collection with the unique items. You can also use a callback function as the $key to perform custom uniqueness checks, like case-insensitive checks.

Laravel

Laravel Collection Public API: values

John Koster
John Koster
November 30, 2016

The values method in Laravel can be used to extract only the values from a collection. It creates a new collection with consecutive numerical keys. Here is an example of how to use the values method in PHP. After executing the code, the resulting collection will contain the values without the original keys.

Laravel

Laravel Collection Public API: where

John Koster
John Koster
November 30, 2016

The where method in Laravel allows developers to filter a collection based on a key-value pair. It returns a new collection with items that match the given criteria. The method supports various operators such as equality, inequality, and comparisons. In older versions of Laravel, the filter method can be used for similar functionality.

Laravel

Laravel Collection Public API: whereIn

John Koster
John Koster
November 30, 2016

The whereIn method filters a collection based on a specific key and an array of possible values for that key. By default, it checks the types of the values in the collection against the types in the given array. The method returns a new, modified collection without changing the original instance. To use whereIn, you simply call it on a collection and pass the desired key and values as arguments, optionally specifying $strict to control type-checking behavior.

Laravel

Laravel Collection Public API: whereInLoose

John Koster
John Koster
November 30, 2016

The whereInLoose method in Laravel's collection allows you to filter the collection based on a given key and an array of possible values, without performing type checking. It is similar to the whereIn method, but does not require a third argument. This method returns a new modified Collection instance, without changing the original instance.

Laravel

Laravel Collection Public API: whereLoose

John Koster
John Koster
November 30, 2016

The whereLoose method, similar to the where method, filters a collection by checking if a given key has a value equal to the provided value. The key difference is that whereLoose does not enforce strict data type checks. This method returns a new Collection instance without modifying the original collection. In the example provided, using whereLoose('age', 2) would return a new collection with two items that match the search criteria, including items with different data types for the age key. The whereLoose method can also be achieved using the where method with optional parameters.

Laravel

Laravel Collection Public API: zip

John Koster
John Koster
November 30, 2016

The zip method in Laravel's Collection class is used to merge the values of an array with the values in the Collection at the corresponding index. It returns a new Collection instance and does not modify the original collection. Multiple arguments can be passed to zip, and they can be any data type that can be converted to an array. In case of different array lengths, null values are used for missing items, and additional items create new Collection instances.

Laravel

Laravel Collection Static API: make

John Koster
John Koster
November 30, 2016

The make static method in the Collection class allows you to create a new instance of a collection with a provided array of items. This method is useful when you want to create a collection without the need for a full constructor call.

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