PHPackages                             webfox/laravel-inertia-dataproviders - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. webfox/laravel-inertia-dataproviders

Abandoned → [foxbytehq/laravel-inertia-dataproviders](/?search=foxbytehq%2Flaravel-inertia-dataproviders)Library[Templating &amp; Views](/categories/templating)

webfox/laravel-inertia-dataproviders
====================================

Data providers to encapsulate logic for Inertia views

v3.0.0(9mo ago)215.8k↓80.6%1[1 issues](https://github.com/foxbytehq/laravel-inertia-dataproviders/issues)MITPHPPHP ^8.2CI passing

Since May 4Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/foxbytehq/laravel-inertia-dataproviders)[ Packagist](https://packagist.org/packages/webfox/laravel-inertia-dataproviders)[ Docs](https://github.com/foxbytehq/laravel-inertia-dataproviders)[ RSS](/packages/webfox-laravel-inertia-dataproviders/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (13)Versions (22)Used By (0)

Laravel Data Providers for Inertia.js
=====================================

[](#laravel-data-providers-for-inertiajs)

[![Latest Version on Packagist](https://camo.githubusercontent.com/19e5ebb46243bcdd251893b30ba892600fadee8354cc2e8a38dc64520def07f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f786279746568712f6c61726176656c2d696e65727469612d6461746170726f7669646572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxbytehq/laravel-inertia-dataproviders)[![Total Downloads](https://camo.githubusercontent.com/6f43d8c5738630ee55dcfd8f05dcecb09679c69d249d9321d7328ff1212766a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f786279746568712f6c61726176656c2d696e65727469612d6461746170726f7669646572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxbytehq/laravel-inertia-dataproviders)

Data providers encapsulate logic for Inertia views, keep your controllers clean and simple.

Installation
------------

[](#installation)

Install this package via composer:

```
composer require foxbytehq/laravel-inertia-dataproviders
```

Optionally publish the configuration file:

```
php artisan vendor:publish --provider="Foxbyte\InertiaDataProviders\InertiaDataProvidersServiceProvider"
```

We assume you've already got the Inertia adapter for Laravel installed.

What Problem Does This Package Solve?
-------------------------------------

[](#what-problem-does-this-package-solve)

Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation &amp; 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 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.

No more 40 line controller methods for fetching data!

Usage
-----

[](#usage)

### Make Command

[](#make-command)

Creating a new Inertia Data Provider is easy with the make:data-provider command.

#### Command:

[](#command)

```
php artisan make:data-provider {name}
```

#### Arguments:

[](#arguments)

- `{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).
- `{--force}`: Overwrite the data provider if it already exists.

#### Example Usage:

[](#example-usage)

```
php artisan make:data-provider UserProfileDataProvider
```

This will generate a UserProfileDataProvider in the `app/Http/DataProviders` directory.

Tip

Using Laravel Idea? There's a "Create Inertia Data Provider" action available!

### Using a Data Provider

[](#using-a-data-provider)

Data providers take advantage of the fact that `Inertia::render` can accept an `Arrayable`. They can also be used as discrete attributes in the data array.

```
use App\Models\Demo;
use App\DataProviders\DemoDataProvider;

class DemoController extends Controller
{
    public function show(Demo $demo)
    {
        return Inertia::render('DemoPage', new DemoDataProvider($demo));
    }

    public function edit(Demo $demo)
    {
        return Inertia::render('DemoPage', [
          'some' => 'data',
          'more' => 'data',
          'demo' => new DemoDataProvider($demo),
        ]);
    }
}
```

### What Does a Data Provider Look Like?

[](#what-does-a-data-provider-look-like)

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.

```
