PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Remove ?page from query string

Solved
haleemhosein

May 8th, 2024 07:53 PM

I'm working on an Angular app and I have added a notifications off-canvas drawer that uses Livewire 2. However, when I click the Next page button, page is appended to the query string and the NG application performs a page navigation.

  • Page with notification drawer open URL:

    • http://demo.happykids.test/admin#/dashboard
  • After clicking Next page, new URL:

    • http://demo.happykids.test/admin?page=2#/dashboard

I was wondering if disabling the page query param is the way to solve this. Any help would be greatly appreciated.

bobbyiliev

May 9th, 2024 05:18 AM

Best Answer

Hey!

I've not tested this but I think that you might be able to just add the queryString property to your Livewire component and specify the parameters to synchronize without query strings, eg:

// YourComponent.php
namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithPagination;

class YourComponent extends Component
{
    use WithPagination;

    // Add the queryString property
    protected $queryString = [];

    // Your other component logic
    public function render()
    {
        $data = Model::paginate(10);

        return view('livewire.your-component', ['data' => $data]);
    }
}

At least this is what I can tell from their docs:

https://laravel-livewire.com/docs/2.x/query-string

Let me know if this works!

haleemhosein

May 9th, 2024 05:49 AM

Hi Bobby, thanks for replying.

I tried adding the protected query string property but no luck. The ?page param is still appended 😞

bobbyiliev

May 9th, 2024 06:28 AM

Hey!

Ah yes indeed, I was unable to get that working on my end as well.

Unfortunately I don't see a way to do that when using the paginate method :/

Another option here for the moment would be to build a simple custom pagination method instead. Here is a quick example that I've tested and is working:

  • Sample Livewire controller method:
<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\User;

class Users extends Component
{
    public $perPage = 1;
    public $currentPage = 1;

    protected $queryString = [];

    public function gotoPage($page)
    {
        $this->currentPage = $page;
        $this->dispatch('pageChanged', $page);
        // Use emit with Livewire v2
    }

    public function previousPage()
    {
        if ($this->currentPage > 1) {
            $this->gotoPage($this->currentPage - 1);
        }
    }

    public function nextPage()
    {
        $this->gotoPage($this->currentPage + 1);
    }

    public function getTotalPages()
    {
        return ceil(User::count() / $this->perPage);
    }

    public function render()
    {
        $totalPages = $this->getTotalPages();
        $offset = ($this->currentPage - 1) * $this->perPage;

        $users = User::skip($offset)
            ->take($this->perPage)
            ->get();

        return view('livewire.users', compact('users', 'totalPages'));
    }
}
  • Livewire view:
<div>
    @foreach ($users as $user)
        <div class="flex items-center justify-between p-4 bg-white border-b border-gray-200">
            <div class="flex items">
                <img class="w-10 h-10 rounded-full" src="{{ $user->profile_photo_url }}" alt="{{ $user->name }}">
                <div class="ml-2">
                    <div class="text-sm font-medium text-gray-900">{{ $user->name }}</div>
                    <div class="text-sm text-gray-500">{{ $user->email }}</div>
                </div>
            </div>
        </div>
    @endforeach

    <div class="mt-4">
        <!-- Custom Pagination -->
        <nav class="flex items-center justify-between">
            @if ($currentPage > 1)
                <button
                    wire:click="previousPage"
                    class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 hover:bg-gray-50"
                >
                    Previous
                </button>
            @else
                <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-not-allowed">
                    Previous
                </span>
            @endif

            <span class="text-sm text-gray-700">
                Page {{ $currentPage }} of {{ $totalPages }}
            </span>

            @if ($currentPage < $totalPages)
                <button
                    wire:click="nextPage"
                    class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 hover:bg-gray-50"
                >
                    Next
                </button>
            @else
                <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-not-allowed">
                    Next
                </span>
            @endif
        </nav>
    </div>
</div>

<script>
document.addEventListener('livewire:load', function () {
    Livewire.on('pageChanged', function (page) {
        const url = new URL(window.location.href);
        url.hash = `#/dashboard`;
        history.pushState(null, '', url.toString());
    });
});
</script>

You can also consider raising a feature request on the Livewire repo to ask for the pagination query parameter to be configurable so it could possibly be disabled with just a flag.

Let me know how it goes!

Best,

Bobby

haleemhosein

May 12th, 2024 09:07 AM

Hi Bobby. Sorry for the late reply, I got caught up with other things. Thank you for all the help. Your first reply worked, and it disable ?page from the query string.

I made a mistake and used the default Laravel paginator markup which had a tags for the previous and next buttons. I changed the a tags to use href='#' and added wire:click for the Next and Previous buttons. However, this caused the Angular app to browse to the home page http://demo.happykids.test/admin?page=2#/dashboard. I then changed the a tags to buttons and I'm happy to report that it's working as expected 🙌 🥳

bobbyiliev

May 13th, 2024 01:41 AM

Hey!

Awesome! Happy to hear that it is all working!

Good luck with your project 👏

- Bobby

Report
1