Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Xanger's avatar

Skipping elements in a query

In this Livewire code I have a query redundancy so that I can indicate which id the query should start from, however, this creates a repetition of the query for me.

My homepage to make you understand is structured like this:

  • 10 articles order desc
  • other element
  • More articles, skipping those 10 from before.

the code I'm using now is:

        $articoli = Articoli::orderByDesc('published_at')
            ->limit(12)
            ->get();
        $articles = Articoli::orderByDesc('published_at')
            ->paginate($this->perPage);

        return view('livewire.article-list', [
            'articles' => $articles,
        ]);

I tried using skip() or offeset() on $articles But it always keeps starting from the first content....

0 likes
3 replies
Snapey's avatar

pagination will also use those limit and offset in order to paginate.

You can amend your query like;

        $articles = Articoli::orderByDesc('published_at')
			->whereNotIn('id', $articoli->pluck('id'))
            ->paginate($this->perPage);

This will exclude the initial 10 from the paginated results.

Xanger's avatar

Thanks for the help @Snapey

https://postimg.cc/7J9FGs5C

In doing so, however, the situation remains similar to my current one, it always runs a query just for counting, I wanted to find a way to do everything just with a query

Snapey's avatar

@Xanger yes. pagination always runs a query for counting.

This is so that it can show the number of pages.

You can switch to simple pagination which then only does one query but then you will only have previous and next navigation buttons.

Please or to participate in this conversation.