How to get the current URL in a Livewire component

In Laravel, you can get the current URL by running url()->current(). However, Livewire doesn't work like that: after subsequent Livewire requests, the “current URL” will be equal to the internal Livewire URL, not the actual page URL.

An easy solution to this is to save the current URL when the component is first loaded, using the mount method. Here is a part of the Livewire component:

 

public $currentUrl;

public function mount()
{
    $this->currentUrl = url()->current();
}

 

Now, you can use $currentUrl in your Livewire component instead of url()->current(), and it will work as expected.