ebess/laravel-inertia-shared-data-provider

Provide shared data to inertia js application in laravel

1.0.0 2021-11-04 16:19 UTC

This package is auto-updated.

Last update: 2022-07-04 17:55:52 UTC


README

This package enables you to share inertia data easy.

Installation

Install via composer

composer require ebess/laravel-inertia-shared-data-provider

Add middleware before the inertia request middleware

    protected $middlewareGroups = [
        'web' => [
            // ...
            \Ebess\LaravelInertiaSharedDataProvider\Http\Middleware\InertiaSharedData::class,
            \App\Http\Middleware\HandleInertiaRequests::class,
        ],
    ];

Usage

Implement shared data provider

use Ebess\LaravelInertiaSharedDataProvider\Contracts\InertiaSharedDataProvider;

class FooBarSharedDataProvider implements InertiaSharedDataProvider
{
    public function provide(Request $request): array
    {
        return [
            'foo' => 'bar',
        ];
    }
}

Tag the provider

use Ebess\LaravelInertiaSharedDataProvider\Contracts\InertiaSharedDataProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->tag(
            FooBarSharedDataProvider::class,
            InertiaSharedDataProvider::TAG_NAME
        );
    }
}

Use shared data in frontend

<template>
    <div>shared data: {{ $page.props.foo }}</div>
</template>