From f9393d36e5b5cdf56d6d485c943a2d2a73f2ed4b Mon Sep 17 00:00:00 2001 From: Ahmed Alaa Date: Tue, 8 Apr 2025 14:16:51 +0200 Subject: [PATCH 1/2] Document fromJson method on collections docs --- collections.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/collections.md b/collections.md index 9cf8a828d5c..37da8daec81 100644 --- a/collections.md +++ b/collections.md @@ -35,6 +35,8 @@ As mentioned above, the `collect` helper returns a new `Illuminate\Support\Colle $collection = collect([1, 2, 3]); ``` +You may also create a collection using [make](#method-make) and [fromJson](#method-fromjson) methods. + > [!NOTE] > The results of [Eloquent](/docs/{{version}}/eloquent) queries are always returned as `Collection` instances. @@ -163,6 +165,7 @@ For the majority of the remaining collection documentation, we'll discuss each m [lazy](#method-lazy) [macro](#method-macro) [make](#method-make) +[fromJson](#method-fromjson) [map](#method-map) [mapInto](#method-mapinto) [mapSpread](#method-mapspread) @@ -1697,6 +1700,29 @@ The static `macro` method allows you to add methods to the `Collection` class at The static `make` method creates a new collection instance. See the [Creating Collections](#creating-collections) section. +```php +use Illuminate\Support\Collection; + +$collection = Collection::make([1, 2, 3]); +``` + + +#### `fromJson()` {.collection-method} + +The static `fromJson` method creates a new collection instance by decoding a JSON string using `json_decode` PHP function. See the [Creating Collections](#creating-collections) section. + +```php +use Illuminate\Support\Collection; + +$json = json_encode([ + 'name' => 'Taylor Otwell', + 'role' => 'Developer', + 'status' => 'Active', +]); + +$collection = Collection::fromJson($json); +``` + #### `map()` {.collection-method} From fa314e36b161d6480921c2380ddce809a4408bb9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 8 Apr 2025 10:10:48 -0500 Subject: [PATCH 2/2] formatting --- collections.md | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/collections.md b/collections.md index 37da8daec81..d1a11fe3c33 100644 --- a/collections.md +++ b/collections.md @@ -35,7 +35,7 @@ As mentioned above, the `collect` helper returns a new `Illuminate\Support\Colle $collection = collect([1, 2, 3]); ``` -You may also create a collection using [make](#method-make) and [fromJson](#method-fromjson) methods. +You may also create a collection using the [make](#method-make) and [fromJson](#method-fromjson) methods. > [!NOTE] > The results of [Eloquent](/docs/{{version}}/eloquent) queries are always returned as `Collection` instances. @@ -146,6 +146,7 @@ For the majority of the remaining collection documentation, we'll discuss each m [flip](#method-flip) [forget](#method-forget) [forPage](#method-forpage) +[fromJson](#method-fromjson) [get](#method-get) [groupBy](#method-groupby) [has](#method-has) @@ -165,7 +166,6 @@ For the majority of the remaining collection documentation, we'll discuss each m [lazy](#method-lazy) [macro](#method-macro) [make](#method-make) -[fromJson](#method-fromjson) [map](#method-map) [mapInto](#method-mapinto) [mapSpread](#method-mapspread) @@ -1249,6 +1249,23 @@ $chunk->all(); // [4, 5, 6] ``` + +#### `fromJson()` {.collection-method} + +The static `fromJson` method creates a new collection instance by decoding a given JSON string using the `json_decode` PHP function: + +```php +use Illuminate\Support\Collection; + +$json = json_encode([ + 'name' => 'Taylor Otwell', + 'role' => 'Developer', + 'status' => 'Active', +]); + +$collection = Collection::fromJson($json); +``` + #### `get()` {.collection-method} @@ -1706,23 +1723,6 @@ use Illuminate\Support\Collection; $collection = Collection::make([1, 2, 3]); ``` - -#### `fromJson()` {.collection-method} - -The static `fromJson` method creates a new collection instance by decoding a JSON string using `json_decode` PHP function. See the [Creating Collections](#creating-collections) section. - -```php -use Illuminate\Support\Collection; - -$json = json_encode([ - 'name' => 'Taylor Otwell', - 'role' => 'Developer', - 'status' => 'Active', -]); - -$collection = Collection::fromJson($json); -``` - #### `map()` {.collection-method}