Search code examples
phplaravellaravel-livewire

Laravel Livewire 2.x is adding query string to URL how to disable it


I want to have clean URL and my table was working fine untill I update the Livewire now my table is adding query string like ?page=2 from page no2

all code is same like before, after searching I have add this in Livewire controller namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
class ContactsTable extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';
    protected $paginationQueryStringEnabled = false;

but its still showing Query String in URL how can I disable this?

Thanks


Solution

  • In order to prevent the default page query string being appended to the browser you can do the following:

    WithPagination.php:

    public function getQueryString()
    {
        return array_merge(['page' => ['except' => 1]], $this->queryString);
    }
    

    As you can see by default it adds page to the queryString property.

    To overwrite this behavior you can add the following method to your component:

    use Livewire\Component;
    use Livewire\WithPagination;
    
    class ContactsTable extends Component
    {
        use WithPagination;
    
        protected $paginationTheme = 'bootstrap';
    
        public function getQueryString()
        {
            return [];
        }
    }
    

    Here we overwrite the getQueryString method that was defined in the WithPagination trait and set it to an empty array.