Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## v1.3.0 - 2023-06-22

### What's Changed

- Added `toNestedArray` method

## v1.2.0 - 2023-02-22

### What's Changed
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ class DemoController extends Controller
}
```

If you need to return the entire dataset as an array, for instance for use in JSON responses, you can use `toNestedArray()

```php
use App\Models\Demo;
use Illuminate\Http\Request;
use App\DataProviders\TabDataProvider;
use App\DataProviders\DemoDataProvider;
use App\DataProviders\CreateVenueDataProvider;

class DemoController extends Controller
{
public function show(Request $request, Demo $demo)
{
return (new DemoDataProvider($demo))->toNestedArray();
}
}
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
12 changes: 8 additions & 4 deletions src/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class DataProvider implements Arrayable
{
protected array|Arrayable $staticData = [];

protected array $excludedMethods = ['__construct', 'toArray', 'dd', 'dump',];
protected array $excludedMethods = ['__construct', 'toArray', 'toNestedArray', 'dd', 'dump',];

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

public function dump(): static
public function toNestedArray(): array
{
$response = new Response('', []);
$props = $response->resolvePropertyInstances($this->toArray(), request());
VarDumper::dump($props);
return $response->resolvePropertyInstances($this->toArray(), request());
}

public function dump(): static
{
VarDumper::dump($this->toNestedArray());

return $this;
}
Expand Down