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
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.3'
coverage: none

- name: Install composer dependencies
Expand Down
47 changes: 0 additions & 47 deletions .github/workflows/run-tests.yml

This file was deleted.

54 changes: 44 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ We assume you've already got the Inertia adapter for Laravel installed.
Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation & authorization logic and
our display logic is in our views, so why do we still insist on making our controllers handle fetching the data for those views?

This is especially evident in Inertia applications due to the introduction of concepts like lazy props.
This is especially evident in Inertia applications due to the introduction of concepts like lazy and deferred props.

Data providers extract the data composition for your Inertia views into their own classes. Inertia data providers may prove particularly
useful if multiple routes or controllers within your application always needs a particular piece of data.
Expand Down Expand Up @@ -65,15 +65,36 @@ class DemoController extends Controller
Data providers can live anywhere, but we'll use `App/Http/DataProviders` for this example.

The simplest data provider is just a class that extends `DataProvider`, any public methods or properties will be available to the page as data.
```php
<?php
declare(strict_types=1);

namespace App\Http\DataProviders;

use Inertia\LazyProp;
use App\Services\InjectedDependency;
use Webfox\InertiaDataProviders\DataProvider;

class DemoDataProvider extends DataProvider
{
public string $someData = 'data';

public function moreData(): int
{
return time();
}
}
```

A **kitchen sink** data provider might look like this:
A full **kitchen sink** data provider might look like this:

```php
<?php
declare(strict_types=1);

namespace App\Http\DataProviders;

use Inertia\DeferProp;
use Inertia\LazyProp;
use App\Services\InjectedDependency;
use Webfox\InertiaDataProviders\DataProvider;
Expand Down Expand Up @@ -116,16 +137,16 @@ class DemoDataProvider extends DataProvider
}

/*
* If a method returns a `Closure` it will be evaluated as a lazy property.
* ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed
* Additionally the callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved.
* @see https://inertiajs.com/partial-reloads#lazy-data-evaluation
* If a method is typed to return a DeferProp, it will only be evaluated in a deferred request after the page has loaded
* NEVER included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated after the page has loaded.
* Additionally the deferred callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved.
* @see https://inertiajs.com/deferred-props
*/
public function quickLazyExample(): Closure
public function deferredExample(): DeferProp
{
return function(InjectedDependency $example): string {
return $example->formatName($this->demo->user->name);
};
return Inertia::defer(
fn () => $this->demo->aHeavyCalculation()
);
}

/*
Expand All @@ -141,6 +162,19 @@ class DemoDataProvider extends DataProvider
);
}

/*
* If a method returns a `Closure` it will be evaluated as a lazy property.
* ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed
* Additionally the callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved.
* @see https://inertiajs.com/partial-reloads#lazy-data-evaluation
*/
public function quickLazyExample(): Closure
{
return function(InjectedDependency $example): string {
return $example->formatName($this->demo->user->name);
};
}

/*
* `protected` and `private` methods are not available to the page
*/
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^11.0"
"illuminate/contracts": "^11.0|^12.0"
},
"require-dev": {
"inertiajs/inertia-laravel": "<2",
"inertiajs/inertia-laravel": "^1.0|^2.0",
"jetbrains/phpstorm-attributes": "^1.0",
"nunomaduro/collision": "^v8.1",
"larastan/larastan": "^v2.9",
Expand Down
3 changes: 2 additions & 1 deletion src/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Inertia\DeferProp;
use Inertia\LazyProp;
use Inertia\Response;
use ReflectionClass;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function toArray(): array
->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods))
->mapWithKeys(function (ReflectionMethod $method) {
$returnType = $method->getReturnType();
if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [LazyProp::class, Closure::class])) {
if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [DeferProp::class, LazyProp::class, Closure::class])) {
return [$method->name => $method->invoke($this)];
}

Expand Down