Hi, everyone! My project uses Laravel + livewire. I'm trying to use paginator with the Livewire component.
The problem is the following, when the page is loaded for the first time, the mount() function is executed, and the paginator works fine, i.e. /user/service/?page=2.
If you start switching between services (click on the tabs), the paginator doesn't work. It replaces all links to /livewire/message/services-all?page=2.
And outputs error:
The GET method is not supported for this route. Supported methods: POST.
I have 2 tabs where all the services and services used by the user.
<a wire:click="displayAllServices"
class="@if($services_status) active @endif "
>All services</a>
<a
wire:click="displayUserServices"
class="@if(!$services_status) active @endif "
>My services</a>
Accordingly, there are 2 methods
use WithPagination;
protected $services_obj;
public $services_status = true;
public $sort_mode = 'DESC';
public $sort_state = "latest";
public function mount()
{
$this->services_obj = ServiceAll::paginate(10);
}
public function displayAllServices()
{
$this->services_obj = ServiceAll::paginate(10);
$this->services_status = true;
}
public function sortUserServices($sort_mode)
{
if (!$this->services_status)
$this->resetPage();
$this->sort_mode = $sort_mode;
$this->displayUserServices();
}
public function displayUserServices()
{
$user = Auth::user();
if ($this->services_status)
$this->resetPage();
if($this->sort_mode === 'ASC') {
$this->services_obj = Service::where('user_id', $user->user_id)
->whereNot('kind', 'analysis')
->orderBy('date', 'ASC')
->paginate(10);
$this->sort_state = "oldest";
}else {
$this->services_obj = Service::where('user_id', $user->user_id)
->whereNot('kind', 'analysis')
->orderBy('date', 'DESC')
->paginate(10);
$this->sort_state = "latest";
}
$this->services_status = false;
}
public function render()
{
return view('livewire.services-all', ['services' => $this->services_obj]);
}
And pagination in 'livewire.services-all':
{{ $services->links('vendor.pagination.custom-pagination', ['route' => route('profile.services')]) }}
As well, I have custom pagination, but I guess it's ok, since it worked correctly on vanilla Laravel, without livewire