├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── inertia-dataproviders.php ├── ide.json ├── src ├── AttributeNameFormatters │ ├── AsWritten.php │ ├── AttributeNameFormatter.php │ ├── CamelCase.php │ └── SnakeCase.php ├── DataProvider.php ├── DataProviderCollection.php ├── InertiaDataProviderMakeCommand.php └── InertiaDataProvidersServiceProvider.php └── stubs └── inertia-data-provider.stub /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-inertia-dataproviders` will be documented in this file. 4 | 5 | ## v2.2.0 - 2025-04-30 6 | 7 | ### What's Changed 8 | 9 | * Add make:data-provider command. by @hailwood in https://github.com/webfox/laravel-inertia-dataproviders/pull/28 10 | 11 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v2.1.0...v2.2.0 12 | 13 | ## v2.1.0 - 2025-02-14 14 | 15 | ### What's Changed 16 | 17 | * Bump dependabot/fetch-metadata from 2.2.0 to 2.3.0 by @dependabot in https://github.com/webfox/laravel-inertia-dataproviders/pull/23 18 | * Add Laravel 12 support. by @hailwood in https://github.com/webfox/laravel-inertia-dataproviders/pull/25 19 | * Add support for Inertia deferred props. by @hailwood in https://github.com/webfox/laravel-inertia-dataproviders/pull/24 20 | 21 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v2.0.1...v2.1.0 22 | 23 | ## v2.0.1 - 2025-01-13 24 | 25 | * Remove redundant filter expression 26 | 27 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v2.0.0...v2.0.1 28 | 29 | ## v2.0.0 - 2024-09-20 30 | 31 | * Closure serialization bugfix. 32 | * Drop support for Laravel 9, 10 33 | 34 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.6.1...v1.6.2 35 | 36 | ## v1.6.1 - 2024-06-21 37 | 38 | * Attribute name formatting bugfix. 39 | 40 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.6.0...v1.6.1 41 | 42 | ## v1.6.0 - 2024-06-21 43 | 44 | ### What's Changed 45 | 46 | * Add support for attribute name formatting. by @hailwood in https://github.com/webfox/laravel-inertia-dataproviders/pull/20 47 | 48 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.5.0...v1.6.0 49 | 50 | ## v1.5.0 - 2024-03-12 51 | 52 | Add Laravel 11 support by @hailwood 53 | 54 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.4.0...v1.5.0 55 | 56 | ## v1.4.0 - 2024-02-27 57 | 58 | ### What's Changed 59 | 60 | * Support returning a dataprovider directly from a controller. by @hailwood in https://github.com/webfox/laravel-inertia-dataproviders/pull/15 61 | 62 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.3.1...v1.4.0 63 | 64 | ## V1.3.1 - 2023-06-22 65 | 66 | ### What's Changed 67 | 68 | - FIX support laravel 9 by @SuryaWebfox in https://github.com/webfox/laravel-inertia-dataproviders/pull/11 69 | 70 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.3.0...v1.3.1 71 | 72 | ## v1.3.0 - 2023-06-22 73 | 74 | ### What's Changed 75 | 76 | - Added `toNestedArray` method 77 | 78 | ## v1.2.0 - 2023-02-22 79 | 80 | ### What's Changed 81 | 82 | - Laravel 10 Support by @Jim-Webfox in https://github.com/webfox/laravel-inertia-dataproviders/pull/6 83 | 84 | ### New Contributors 85 | 86 | - @Jim-Webfox made their first contribution in https://github.com/webfox/laravel-inertia-dataproviders/pull/6 87 | 88 | **Full Changelog**: https://github.com/webfox/laravel-inertia-dataproviders/compare/v1.1.0...v1.2.0 89 | 90 | ## v1.1.0 - 2022-09-14 91 | 92 | Add `dd` and `dump` methods to the DataProvider class 93 | 94 | ## v1.0.2 - 2022-05-04 95 | 96 | Fix return type check failing against union types. 97 | 98 | ## v1.0.1 - 2022-05-04 99 | 100 | Updates to composer.json to help discoverability 101 | 102 | ## v1.0.0 - 2022-05-04 103 | 104 | Initial release of package! 105 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) webfox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Data Providers for Inertia.js 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/webfox/laravel-inertia-dataproviders.svg?style=flat-square)](https://packagist.org/packages/webfox/laravel-inertia-dataproviders) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/webfox/laravel-inertia-dataproviders.svg?style=flat-square)](https://packagist.org/packages/webfox/laravel-inertia-dataproviders) 5 | 6 | Data providers encapsulate logic for Inertia views, keep your controllers clean and simple. 7 | 8 | ## Installation 9 | 10 | Install this package via composer: 11 | 12 | ```bash 13 | composer require webfox/laravel-inertia-dataproviders 14 | ``` 15 | 16 | Optionally publish the configuration file: 17 | 18 | ```bash 19 | php artisan vendor:publish --provider="Webfox\InertiaDataProviders\InertiaDataProvidersServiceProvider" 20 | ```` 21 | 22 | We assume you've already got the Inertia adapter for Laravel installed. 23 | 24 | ## What Problem Does This Package Solve? 25 | Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation & authorization logic and 26 | our display logic is in our views, so why do we still insist on making our controllers handle fetching the data for those views? 27 | 28 | This is especially evident in Inertia applications due to the introduction of concepts like lazy and deferred props. 29 | 30 | Data providers extract the data composition for your Inertia views into their own classes. Inertia data providers may prove particularly 31 | useful if multiple routes or controllers within your application always needs a particular piece of data. 32 | 33 | No more 40 line controller methods for fetching data! 34 | 35 | ## Usage 36 | 37 | ### Make Command 38 | 39 | Creating a new Inertia Data Provider is easy with the make:data-provider command. 40 | 41 | #### Command: 42 | 43 | ```Bash 44 | php artisan make:data-provider {name} 45 | ``` 46 | 47 | #### Arguments: 48 | 49 | * `{name}`: The name of the enum class to be created (e.g., OrderStatus). The command will automatically append "Enum" to the name (e.g., OrderStatusEnum). 50 | * `{--force}`: Overwrite the data provider if it already exists. 51 | 52 | #### Example Usage: 53 | 54 | ``` Bash 55 | php artisan make:data-provider UserProfileDataProvider 56 | ``` 57 | 58 | This will generate a UserProfileDataProvider in the `app/Http/DataProviders` directory. 59 | 60 | > [!TIP] 61 | > Using Laravel Idea? There's a "Create Inertia Data Provider" action available! 62 | 63 | ### Using a Data Provider 64 | Data providers take advantage of the fact that `Inertia::render` can accept an `Arrayable`. 65 | They can also be used as discrete attributes in the data array. 66 | 67 | ```php 68 | use App\Models\Demo; 69 | use App\DataProviders\DemoDataProvider; 70 | 71 | class DemoController extends Controller 72 | { 73 | public function show(Demo $demo) 74 | { 75 | return Inertia::render('DemoPage', new DemoDataProvider($demo)); 76 | } 77 | 78 | public function edit(Demo $demo) 79 | { 80 | return Inertia::render('DemoPage', [ 81 | 'some' => 'data', 82 | 'more' => 'data', 83 | 'demo' => new DemoDataProvider($demo), 84 | ]); 85 | } 86 | } 87 | ``` 88 | 89 | ### What Does a Data Provider Look Like? 90 | 91 | Data providers can live anywhere, but we'll use `App/Http/DataProviders` for this example. 92 | 93 | The simplest data provider is just a class that extends `DataProvider`, any public methods or properties will be available to the page as data. 94 | ```php 95 | staticData = [ 144 | /* 145 | * This will be available to the page as `title` 146 | */ 147 | 'title' => $this->calculateTitle($demo), 148 | ]; 149 | } 150 | 151 | /* 152 | * All public methods are automatically evaluated as data and provided to the page. 153 | * ALWAYS included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated 154 | * This would be available to the page as `someData`. 155 | * Additionally these methods are resolved through Laravel's service container, so any parameters will be automatically resolved. 156 | */ 157 | public function someData(InjectedDependency $example): array 158 | { 159 | return [ 160 | 'some' => $example->doThingWith('some'), 161 | 'more' => 'data', 162 | ]; 163 | } 164 | 165 | /* 166 | * If a method is typed to return a DeferProp, it will only be evaluated in a deferred request after the page has loaded 167 | * NEVER included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated after the page has loaded. 168 | * Additionally the deferred callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved. 169 | * @see https://inertiajs.com/deferred-props 170 | */ 171 | public function deferredExample(): DeferProp 172 | { 173 | return Inertia::defer( 174 | fn () => $this->demo->aHeavyCalculation() 175 | ); 176 | } 177 | 178 | /* 179 | * If a method is typed to return a LazyProp, it will only be evaluated when requested following inertia's rules for lazy data evaluation 180 | * NEVER included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed 181 | * Additionally the lazy callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved. 182 | * @see https://inertiajs.com/partial-reloads#lazy-data-evaluation 183 | */ 184 | public function lazyExample(): LazyProp 185 | { 186 | return Inertia::lazy( 187 | fn (InjectedDependency $example) => $example->aHeavyCalculation($this->demo) 188 | ); 189 | } 190 | 191 | /* 192 | * If a method returns a `Closure` it will be evaluated as a lazy property. 193 | * ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed 194 | * Additionally the callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved. 195 | * @see https://inertiajs.com/partial-reloads#lazy-data-evaluation 196 | */ 197 | public function quickLazyExample(): Closure 198 | { 199 | return function(InjectedDependency $example): string { 200 | return $example->formatName($this->demo->user->name); 201 | }; 202 | } 203 | 204 | /* 205 | * `protected` and `private` methods are not available to the page 206 | */ 207 | protected function calculateTitle(Demo $demo): string 208 | { 209 | return $demo->name . ' Demo'; 210 | } 211 | 212 | } 213 | ``` 214 | 215 | ### Using Multiple Data Providers 216 | Sometimes you might find yourself wanting to return multiple DataProviders `DataProvider::collection` is the method for you. 217 | Each DataProvider in the collection will be evaluated and merged into the page's data, later values from DataProviders will override earlier DataProviders. 218 | 219 | ```php 220 | use App\Models\Demo; 221 | use App\DataProviders\TabDataProvider; 222 | use App\DataProviders\DemoDataProvider; 223 | 224 | class DemoController extends Controller 225 | { 226 | public function show(Demo $demo) 227 | { 228 | return Inertia::render('DemoPage', DataProvider::collection( 229 | new TabDataProvider(current: 'demo'), 230 | new DemoDataProvider($demo), 231 | )); 232 | } 233 | } 234 | ``` 235 | 236 | You can also conditionally include DataProviders in the collection: 237 | ```php 238 | use App\Models\Demo; 239 | use App\DataProviders\TabDataProvider; 240 | use App\DataProviders\DemoDataProvider; 241 | use App\DataProviders\EditDemoDataProvider; 242 | use App\DataProviders\CreateVenueDataProvider; 243 | 244 | class DemoController extends Controller 245 | { 246 | public function show(Demo $demo) 247 | { 248 | return Inertia::render('DemoPage', DataProvider::collection( 249 | new TabDataProvider(current: 'demo'), 250 | new DemoDataProvider($demo), 251 | )->when($demo->has_venue, function (DataProviderCollection $collection) use($demo) { 252 | $collection->push(new CreateVenueDataProvider($demo)); 253 | }) 254 | ->unless($demo->locked, function (DataProviderCollection $collection) use($demo) { 255 | $collection->push(new EditDemoDataProvider($demo)); 256 | })); 257 | } 258 | } 259 | ``` 260 | 261 | Or you can use the `DataProviderCollection::add` method to add a DataProvider to the collection later: 262 | ```php 263 | use App\Models\Demo; 264 | use App\DataProviders\TabDataProvider; 265 | use App\DataProviders\DemoDataProvider; 266 | use App\DataProviders\CreateVenueDataProvider; 267 | 268 | class DemoController extends Controller 269 | { 270 | public function show(Demo $demo) 271 | { 272 | $pageData = DataProvider::collection( 273 | new TabDataProvider(current: 'demo'), 274 | new DemoDataProvider($demo), 275 | ); 276 | 277 | if($demo->has_venue) { 278 | $pageData->add(new CreateVenueDataProvider($demo)); 279 | } 280 | 281 | return Inertia::render('DemoPage', $pageData); 282 | } 283 | } 284 | ``` 285 | 286 | If you need to return the entire dataset as an array, for instance for use in JSON responses, you can use `toNestedArray() 287 | 288 | ```php 289 | use App\Models\Demo; 290 | use Illuminate\Http\Request; 291 | use App\DataProviders\TabDataProvider; 292 | use App\DataProviders\DemoDataProvider; 293 | use App\DataProviders\CreateVenueDataProvider; 294 | 295 | class DemoController extends Controller 296 | { 297 | public function show(Request $request, Demo $demo) 298 | { 299 | return (new DemoDataProvider($demo))->toNestedArray(); 300 | } 301 | } 302 | ``` 303 | 304 | ## Attribute Name Formatting 305 | The attribute name format can be configured in the configuration file by setting the `attribute_name_formatter`. 306 | The package ships with three formatters under the namespace `\Webfox\InertiaDataProviders\AttributeNameFormatters` but you are free to create your own. 307 | 308 | ### AsWritten 309 | This is the default formatter. The output attribute name will be the same as the input name. 310 | E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `someData` and `more_data`. 311 | 312 | ### SnakeCase 313 | This formatter will convert the attribute name to snake_case. 314 | E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `some_data` and `more_data`. 315 | 316 | ### CamelCase 317 | This formatter will convert the attribute name to camelCase. 318 | E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `someData` and `moreData`. 319 | 320 | ## Changelog 321 | 322 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 323 | 324 | ## Contributing 325 | 326 | We welcome all contributors to the project. 327 | 328 | ## License 329 | 330 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 331 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webfox/laravel-inertia-dataproviders", 3 | "description": "Data providers to encapsulate logic for Inertia views", 4 | "keywords": [ 5 | "webfox", 6 | "laravel", 7 | "inertia" 8 | ], 9 | "homepage": "https://github.com/webfox/laravel-inertia-dataproviders", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Webfox Developments Ltd", 14 | "email": "developers@webfox.co.nz", 15 | "role": "Developer" 16 | } 17 | ], 18 | "suggest": { 19 | "inertiajs/inertia-laravel": "Needed for basic inertia functionality" 20 | }, 21 | "require": { 22 | "php": "^8.2", 23 | "spatie/laravel-package-tools": "^1.14.0", 24 | "illuminate/contracts": "^11.0|^12.0" 25 | }, 26 | "require-dev": { 27 | "inertiajs/inertia-laravel": "^1.0|^2.0", 28 | "jetbrains/phpstorm-attributes": "^1.0", 29 | "nunomaduro/collision": "^v8.1", 30 | "larastan/larastan": "^v2.9", 31 | "orchestra/testbench": "^v9.0", 32 | "pestphp/pest": "^v2.34", 33 | "pestphp/pest-plugin-laravel": "^v2.3", 34 | "phpstan/extension-installer": "^1.1", 35 | "phpstan/phpstan-deprecation-rules": "^1.0", 36 | "phpstan/phpstan-phpunit": "^1.0", 37 | "phpunit/phpunit": "^10.5" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "Webfox\\InertiaDataProviders\\": "src" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "psr-4": { 46 | "Webfox\\InertiaDataProviders\\Tests\\": "tests" 47 | } 48 | }, 49 | "scripts": { 50 | "analyse": "vendor/bin/phpstan analyse", 51 | "test": "vendor/bin/pest", 52 | "test-coverage": "vendor/bin/pest --coverage" 53 | }, 54 | "config": { 55 | "sort-packages": true, 56 | "allow-plugins": { 57 | "pestphp/pest-plugin": true, 58 | "phpstan/extension-installer": true 59 | } 60 | }, 61 | "extra": { 62 | "laravel": { 63 | "providers": [ 64 | "Webfox\\InertiaDataProviders\\InertiaDataProvidersServiceProvider" 65 | ] 66 | } 67 | }, 68 | "minimum-stability": "dev", 69 | "prefer-stable": true 70 | } 71 | -------------------------------------------------------------------------------- /config/inertia-dataproviders.php: -------------------------------------------------------------------------------- 1 | \Webfox\InertiaDataProviders\AttributeNameFormatters\AsWritten::class, 5 | ]; -------------------------------------------------------------------------------- /ide.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json", 3 | "codeGenerations": [ 4 | { 5 | "id": "webfox.create-inertia-data-provider", 6 | "name": "Create Inertia Data Provider", 7 | "classSuffix": "DataProvider", 8 | "files": [ 9 | { 10 | "appNamespace": "Http\\DataProviders", 11 | "name": "${INPUT_CLASS|className|upperCamelCase}.php", 12 | "template": { 13 | "type": "stub", 14 | "path": "/stubs/inertia-data-provider.stub", 15 | "fallbackPath": "stubs/inertia-data-provider.stub", 16 | "parameters": { 17 | "{{ class }}": "${INPUT_CLASS|className|upperCamelCase}", 18 | "{{ namespace }}": "${INPUT_FQN|namespace}" 19 | } 20 | } 21 | } 22 | ] 23 | } 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /src/AttributeNameFormatters/AsWritten.php: -------------------------------------------------------------------------------- 1 | camel()->toString(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/AttributeNameFormatters/SnakeCase.php: -------------------------------------------------------------------------------- 1 | snake()->toString(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/DataProvider.php: -------------------------------------------------------------------------------- 1 | staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData; 34 | $reflectionClass = (new ReflectionClass($this)); 35 | 36 | $convertedProperties = collect($reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC)) 37 | ->filter(fn (ReflectionProperty $property) => ! $property->isStatic()) 38 | ->mapWithKeys(fn (ReflectionProperty $property) => [$property->getName() => $property->getValue($this)]) 39 | ->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value); 40 | 41 | $convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC)) 42 | ->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods)) 43 | ->mapWithKeys(function (ReflectionMethod $method) { 44 | $returnType = $method->getReturnType(); 45 | if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [DeferProp::class, LazyProp::class, Closure::class])) { 46 | return [$method->name => $method->invoke($this)]; 47 | } 48 | 49 | return [$method->name => fn () => app()->call([$this, $method->name])]; 50 | }); 51 | 52 | return collect() 53 | ->merge($staticData) 54 | ->merge($convertedProperties) 55 | ->merge($convertedMethods) 56 | ->mapWithKeys(fn ($value, $key) => [$this->attributeNameFormatter()($key) => $value]) 57 | ->toArray(); 58 | } 59 | 60 | public function toNestedArray(): array 61 | { 62 | $response = new Response('', []); 63 | 64 | return $response->resolvePropertyInstances($this->toArray(), request()); 65 | } 66 | 67 | public function toJson($options = 0): string 68 | { 69 | return json_encode($this->toNestedArray(), $options); 70 | } 71 | 72 | public function dump(): static 73 | { 74 | VarDumper::dump($this->toNestedArray()); 75 | 76 | return $this; 77 | } 78 | 79 | #[\JetBrains\PhpStorm\NoReturn] 80 | public function dd() 81 | { 82 | $this->dump(); 83 | exit(1); 84 | } 85 | 86 | protected function attributeNameFormatter(): AttributeNameFormatter 87 | { 88 | return app()->make(config('inertia-dataproviders.attribute_name_formatter')); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/DataProviderCollection.php: -------------------------------------------------------------------------------- 1 | */ 14 | public Collection $dataProviders; 15 | 16 | public function __construct(DataProvider|array ...$dataProviders) 17 | { 18 | $this->dataProviders = collect(); 19 | $this->add($dataProviders); 20 | } 21 | 22 | public function add(DataProvider|array ...$dataProviders): static 23 | { 24 | $this->dataProviders = $this->dataProviders->concat( 25 | collect($dataProviders) 26 | ->map(fn (DataProvider|array $dataProvider) => Arr::wrap($dataProvider)) 27 | ->flatten() 28 | ->filter() 29 | ); 30 | 31 | return $this; 32 | } 33 | 34 | /** 35 | * @param class-string $dataProvider 36 | */ 37 | public function remove(string $dataProvider): static 38 | { 39 | $this->dataProviders = $this->dataProviders->filter(fn (DataProvider $instance) => $instance::class !== $dataProvider); 40 | 41 | return $this; 42 | } 43 | 44 | public function collection(): Collection 45 | { 46 | return $this->dataProviders; 47 | } 48 | 49 | public function empty(): static 50 | { 51 | $this->dataProviders = collect(); 52 | 53 | return $this; 54 | } 55 | 56 | /** 57 | * @param bool|callable $condition 58 | * @param callable $callback 59 | * @return $this 60 | */ 61 | public function when(bool|callable $condition, callable $callback): static 62 | { 63 | if (value($condition)) { 64 | $callback($this); 65 | } 66 | 67 | return $this; 68 | } 69 | 70 | /** 71 | * @param bool|callable $condition 72 | * @param callable $callback 73 | * @return $this 74 | */ 75 | public function unless(bool|callable $condition, callable $callback): static 76 | { 77 | if (! value($condition)) { 78 | $callback($this); 79 | } 80 | 81 | return $this; 82 | } 83 | 84 | public function toArray(): array 85 | { 86 | return $this->dataProviders 87 | ->flatMap(fn (DataProvider $dataProvider) => $dataProvider->toArray()) 88 | ->all(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/InertiaDataProviderMakeCommand.php: -------------------------------------------------------------------------------- 1 | files->exists($this->getPath($this->qualifyClass($rawName))); 22 | } 23 | 24 | /** 25 | * Get the stub file for the generator. 26 | * 27 | * @return string 28 | */ 29 | protected function getStub() 30 | { 31 | return $this->resolveStubPath('/stubs/inertia-data-provider.stub'); 32 | } 33 | 34 | /** 35 | * Resolve the fully-qualified path to the stub. 36 | * 37 | * @param string $stub 38 | * @return string 39 | */ 40 | protected function resolveStubPath($stub) 41 | { 42 | return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) 43 | ? $customPath 44 | : realpath(__DIR__ . '/..' . $stub); 45 | } 46 | 47 | /** 48 | * Get the default namespace for the class. 49 | * 50 | * @param string $rootNamespace 51 | * @return string 52 | */ 53 | protected function getDefaultNamespace($rootNamespace) 54 | { 55 | return $rootNamespace.'\Http\DataProviders'; 56 | } 57 | 58 | /** 59 | * Get the console command options. 60 | * 61 | * @return array 62 | */ 63 | protected function getOptions() 64 | { 65 | return [ 66 | ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the data provider already exists'], 67 | ]; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/InertiaDataProvidersServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-inertia-dataproviders') 14 | ->hasConfigFile('inertia-dataproviders'); 15 | 16 | $package->hasConsoleCommand(InertiaDataProviderMakeCommand::class); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /stubs/inertia-data-provider.stub: -------------------------------------------------------------------------------- 1 | staticData = [ 13 | ]; 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------