Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vaites's avatar

Pass GET arguments set on resource detail request to Laravel Nova's API

Hi

I want to add some parameters to the detail URL of a resource and make it accesible from the Laravel Nova API. The reason is that I want to show/hide relationships and resource tools depending on GET arguments. Example: http://localhost/nova/resources/posts/1?key=value

All requests to display resource and its relationships and tools are made via ajax without these data. So on the fields method of the resource I can't use $request->get('key'). It is necessary for me to display a resource tool based on arguments that are set after redirecting from an action.

As a workaround I save the arguments using cache on a middleware and read it from the backend.

Is there an easier way to do it?. Does Laravel Nova provide a way to read the current URL when its called through ajax?

Thanks!

0 likes
4 replies
vaites's avatar

Another approach is parse the Referer header value, but it depends on user's browser. If the server has a restrictive Permissions-Policy header will stop working.

vaites's avatar

Finally I solved the problem using Axios Interceptors on Nova.request():

Nova.request().interceptors.request.use
(
    function(config)
    {
        const current = new URL(window.location.href)

        current.searchParams.forEach((value, key) =>
        {
            const append = {[key]: value};

            config.params = {...config.params, ...append};
        });

        return config;
    },
    function(error)
    {
        return Promise.reject(error);
    }
);

This will add all the current parameters to all API calls, so if I load the http://localhost/nova/resources/posts/1?foo=bar URL, the $request variable on my Post::fields() method will have the foo parameter.

Doesn't seems to be the best way to do it, but didn't find a better alternative.

1 like
goellner's avatar

@vaites Where (in which js file) did you add the code code for the interceptor?

vaites's avatar

@goellner You can put it in any file. I created a JS file for all my tools/cards/extras and added it using Nova::script() on NovaServiceProvider.

Please or to participate in this conversation.