Skip to content

Commit 2145907

Browse files
authored
Support returning a dataprovider directly from a controller
1 parent fd7641a commit 2145907

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/DataProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Webfox\InertiaDataProviders;
66

77
use Illuminate\Contracts\Support\Arrayable;
8+
use Illuminate\Contracts\Support\Jsonable;
89
use Inertia\LazyProp;
910
use Inertia\Response;
1011
use ReflectionClass;
@@ -13,11 +14,11 @@
1314
use ReflectionProperty;
1415
use Symfony\Component\VarDumper\VarDumper;
1516

16-
abstract class DataProvider implements Arrayable
17+
abstract class DataProvider implements Arrayable, Jsonable
1718
{
1819
protected array|Arrayable $staticData = [];
1920

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

2223
public static function collection(DataProvider|array ...$dataProviders): DataProviderCollection
2324
{
@@ -56,6 +57,11 @@ public function toNestedArray(): array
5657
return $response->resolvePropertyInstances($this->toArray(), request());
5758
}
5859

60+
public function toJson($options = 0): string
61+
{
62+
return json_encode($this->toNestedArray(), $options);
63+
}
64+
5965
public function dump(): static
6066
{
6167
VarDumper::dump($this->toNestedArray());

0 commit comments

Comments
 (0)