Skip to content

Commit

Permalink
Support returning a dataprovider directly from a controller
Browse files Browse the repository at this point in the history
  • Loading branch information
hailwood authored Feb 27, 2024
1 parent fd7641a commit 2145907
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Webfox\InertiaDataProviders;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Inertia\LazyProp;
use Inertia\Response;
use ReflectionClass;
Expand All @@ -13,11 +14,11 @@
use ReflectionProperty;
use Symfony\Component\VarDumper\VarDumper;

abstract class DataProvider implements Arrayable
abstract class DataProvider implements Arrayable, Jsonable
{
protected array|Arrayable $staticData = [];

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

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

public function toJson($options = 0): string
{
return json_encode($this->toNestedArray(), $options);
}

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

0 comments on commit 2145907

Please sign in to comment.