Skip to content

Commit 1d326c9

Browse files
authored
Merge pull request #9 from SuryaWebfox/main
ADD `toNestedArray` method
2 parents f86ee46 + 2aa89e5 commit 1d326c9

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `laravel-inertia-dataproviders` will be documented in this file.
44

5+
## v1.3.0 - 2023-06-22
6+
7+
### What's Changed
8+
9+
- Added `toNestedArray` method
10+
511
## v1.2.0 - 2023-02-22
612

713
### What's Changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ class DemoController extends Controller
215215
}
216216
```
217217

218+
If you need to return the entire dataset as an array, for instance for use in JSON responses, you can use `toNestedArray()
219+
220+
```php
221+
use App\Models\Demo;
222+
use Illuminate\Http\Request;
223+
use App\DataProviders\TabDataProvider;
224+
use App\DataProviders\DemoDataProvider;
225+
use App\DataProviders\CreateVenueDataProvider;
226+
227+
class DemoController extends Controller
228+
{
229+
public function show(Request $request, Demo $demo)
230+
{
231+
return (new DemoDataProvider($demo))->toNestedArray();
232+
}
233+
}
234+
```
235+
218236
## Changelog
219237

220238
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

src/DataProvider.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class DataProvider implements Arrayable
1717
{
1818
protected array|Arrayable $staticData = [];
1919

20-
protected array $excludedMethods = ['__construct', 'toArray', 'dd', 'dump',];
20+
protected array $excludedMethods = ['__construct', 'toArray', 'toNestedArray', 'dd', 'dump',];
2121

2222
public static function collection(DataProvider|array ...$dataProviders): DataProviderCollection
2323
{
@@ -50,11 +50,15 @@ public function toArray(): array
5050
return collect()->merge($staticData)->merge($convertedProperties)->merge($convertedMethods)->toArray();
5151
}
5252

53-
public function dump(): static
53+
public function toNestedArray(): array
5454
{
5555
$response = new Response('', []);
56-
$props = $response->resolvePropertyInstances($this->toArray(), request());
57-
VarDumper::dump($props);
56+
return $response->resolvePropertyInstances($this->toArray(), request());
57+
}
58+
59+
public function dump(): static
60+
{
61+
VarDumper::dump($this->toNestedArray());
5862

5963
return $this;
6064
}

0 commit comments

Comments
 (0)