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

Dave Wize's avatar

Any way to retrieve the current active tab from a tabs form component?

Hi everyone.

I need to get the current tab of my tab component when the create method runs. But nothing really seems to work


    public ?array $data = [];

    #[Url]
    public ?string $tab;

    public function mount(): void
    {
        $this->form->fill();
    }

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Tabs::make()->schema([
                    Tab::make('Single donation')
                        ->schema([
                            //
                        ])->columns(4),
                    Tab::make('Recurring donation')
                        ->schema([
                          //
                        ])->columns(4),
                ])->persistTabInQueryString()->id('donate')
            ])
            ->statePath('data');
    }

    public function create(): void
    {
        dd( $this->tab, request()->query('tab) );
    }

Now this tab returns the default value but is not getting synced with the URL param, and the request()->query() is always null. Any help is appreciated

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like you're trying to retrieve the current active tab from a tabs form component and synchronize it with the URL parameter. To achieve this, you need to ensure that the tab state is being updated correctly when a new tab is selected and that the URL parameter is being read properly.

Here's a solution that might help you:

  1. Make sure that the persistTabInQueryString method is working correctly. This method should update the URL with the active tab's identifier when a tab is changed.

  2. In your mount method, read the tab parameter from the query string and set it as the active tab.

  3. In your create method, you should be able to access the current tab from the $this->tab property if it's being set correctly.

Here's how you can modify your code:

public function mount(): void
{
    $this->form->fill();

    // Read the 'tab' parameter from the query string and set it as the active tab
    $this->tab = request()->query('tab', $this->tab);
}

public function create(): void
{
    // Now $this->tab should have the current active tab
    dd($this->tab);
}

Make sure that your Tabs component is updating the URL correctly when a tab is changed. If the persistTabInQueryString method is not working as expected, you might need to manually update the URL when a tab is selected.

If you're using JavaScript to handle tab changes, you can update the URL using the history.pushState method:

function onTabChange(tabId) {
    history.pushState({}, '', '?tab=' + tabId);
}

Remember to call this function whenever a tab is changed, passing the appropriate tab identifier.

If you're still having trouble, please provide more information about how your Tabs component works and how it's supposed to update the URL, so that a more precise solution can be given.

Dave Wize's avatar

@LaryAI Not working

Worth mentioning that this is actually on a widget that lives on a resource; it might not have access to the request() of the parent or maybe some other issues... I'm lost

martinbean's avatar

This reply was automagically generated by our local A.I. Think of it as a one-off attempt to instantly solve your problem. It will not respond to further replies.

Please or to participate in this conversation.