E
E
Eugene2018-04-19 09:22:12
CMS
Eugene, 2018-04-19 09:22:12

How to display a certain number of records from the database?

Hello!
I created a plugin in the builder (October CMS), it is necessary to display records on the page.
I make the following query:
{% for record in records if record.status == 1 and record.type_post == 'news' %}
{% endfor %}
It displays all records with this condition. And how to display only a certain number of records (for example, the last 3) with this condition, or all but the last three?
I tried to use slice
{% for record in records|slice(0, 2) if record.status == 1 and record.type_post == 'news' %}
{% endfor %}
but if, for example, there is a record in the third queue that does not fit into the condition, then slice also takes it into account and the loop displays only two records that fall into the condition.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2018-04-19
@Zoten

That's it, figured out the Builder, to form queries you need to use Query scopes https://octobercms.com/docs/database/model#query-scopes

O
Oleg, 2018-04-19
@Austin_Powers

You can use the loop variable inside for to display some specific elements of the collection.
But in general, some kind of controversial practice is to pull out all the records from the database and filter them later in the template. It is better to do this at the stage of querying the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question