Skip to content

[2.x] Introduce ProvidesInertiaProps interface #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: inertia-responsable-prop-type
Choose a base branch
from

Conversation

pascalbaljet
Copy link
Member

This PR is an addition to #746 and inspired by this comment. Given the one-letter difference in interface names, we might have to do some renaming to prevent confusion.

While Inertia::render() already support Arrayable as props, there's still room left to improve how we organize and reuse props, especially for complex pages. This new ProvidesInertiaProps interface has one method that allows you to return multiple props from a class, and then use multiple of these classes to gather your props for a page.

Here's an example of a class implementing this interface:

class LocalizationProps implements ProvidesInertiaProps
{
    public function toInertiaProps(RenderContext $context): iterable
    {
        return [
            'currentLocale' => app()->getLocale(),
            'fallbackLocale' => config('app.fallback_locale', 'en'),
            'availableLocales' => array_keys(config('app.locales', [])),
        ];
    }
}

You can use this directly in the render() and with() methods:

public function index(LocalizationProps $localizationProps)
{
    return Inertia::render('UserProfile', $localizationProps);

    // or...

    return Inertia::render('UserProfile')->with($localizationProps);
}

Even cooler, you can combine these prop classes with your other props in an array (without using the spread operator):

Inertia::render('UserProfile', [
    'user' => auth()->user(),
    $localizationProps,
]);

For complex pages with many reusable props, this can be a nice cleanup:

public function index(LocalizationProps $localizationProps, PermissionsProps $permissionProps)
{
    return Inertia::render('UserProfile', [
        'user' => auth()->user(),
        $localizationProps,
        $permissionProps,
    ]);

    // or...

    return Inertia::render('UserProfile')
        ->with('user', auth()->user())
        ->with($localizationProps)
        ->with($permissionProps);
}

The RenderContext object contains the Request instance and the component name.

@pascalbaljet pascalbaljet changed the base branch from 2.x to inertia-responsable-prop-type June 19, 2025 13:47
@danmatthews
Copy link

Love this @pascalbaljet - using it with ->with will be how I use it i'd think, I think it's a nice backwards compatible QOL improvement along with the other PR.

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