It seems like you're encountering issues with Livewire's pagination and the way you're passing the paginated data to your view. The error messages you're seeing are due to the way you're handling the $services variable.
Here's how you can fix the issues:
-
Remove the
public $services;property from your component class. This property is not needed because you will be fetching the services within therendermethod and passing them directly to the view. -
In your
rendermethod, you're correctly paginating theServicemodel. However, you should not assign the paginated result to a local variable$services. Instead, just return it directly to the view. -
In your Blade view, you should use the
$servicesvariable that is passed from therendermethod. Since it's a paginated result, you can call thelinks()method on it to render the pagination links.
Here's the corrected code:
use Livewire\Component;
use Illuminate\Contracts\View\View;
use Livewire\WithPagination;
use App\Models\Service;
class Services extends Component
{
use WithPagination;
public function render(): View
{
return view('livewire.onyx.services', [
'services' => Service::paginate(10)
]);
}
}
And in your Blade view, you should loop through the $services variable like this:
@foreach ($services as $service)
<!-- Display each service -->
@endforeach
<!-- Pagination Links -->
{{ $services->links() }}
With these changes, your Livewire component should work correctly with pagination. Remember that when using Livewire's WithPagination trait, you don't need to manage the pagination state manually; Livewire takes care of it for you. Just make sure to use the variable passed to the view for both displaying the items and rendering the pagination links.