Skip to content

Conversation

lepikhinb
Copy link
Contributor

@lepikhinb lepikhinb commented May 13, 2024

The PR introduces an ability to persist properties on partial requests.

When making a partial reload, Inertia will only evaluate and include on the response properties, specified in the only array. This adds some extra inconvenience when you need to carry some common data between pages (e.g. current user data).

Inertia::persist('auth.user');
// or
inertia()->persist(['auth.user'])
router.visit(url, {
  only: ['billing'],
})
$props = [
    // Included on partial reload
    'auth' => [
        'user' => new LazyProp(function () {
            return [
                'name' => 'Jonathan Reinink',
                'email' => 'jonathan@example.com',
            ];
        }),
    ],

    // Included on partial reload
    'billing' => [
        //
    ],
    
    // Not included on partial reload
    'data' => [
        //
    ],
];

Alternatively, you can define persistent properties globally in a middleware. These will be available on any request.

class HandleInertiaRequests extends Middleware
{
    protected $persisted = ['auth.user'];

    public function share(Request $request): array
    {
        return array_merge(parent::share($request), [
            'auth' => [
                'user' => new LazyProp(function () {
                    return [
                        'name' => 'Jonathan Reinink',
                        'email' => 'jonathan@example.com',
                    ];
                }),
            ],
        ]);
    }
}

Example use cases:

  • Flash messages
  • Auth data

@lepikhinb lepikhinb marked this pull request as ready for review May 13, 2024 23:51
@lepikhinb lepikhinb changed the title Persistent properties [1.x] Persistent properties May 14, 2024
@taylorotwell taylorotwell merged commit 27fb7e8 into inertiajs:1.x May 17, 2024
@reinink reinink changed the title [1.x] Persistent properties Add persistent properties May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants