Collection

Collection

Adds a collection of model records to the page.

The collection component displays a collection of entries on the page. The collection component can be used on any page, layout or partial.

# Available Properties

The following properties are supported by the component.

PropertyDescription
handleThe handle of the entry blueprint.
recordsPerPageNumber of records to display on a single page. Leave empty to disable pagination.
pageNumberThis value is used to determine what page the user is on.
sortColumnColumn name the records should be ordered by.
sortDirectionDirection the records should be ordered by. Supported values are asc and desc.

# Basic Usage

The following adds a collection of Blog\Post entries to the page. The collection is accessed in Twig by looping the default collection variable.

[collection]
handle = "Blog\Post"
___TWIG0___
    <h1>___TWIG1___</h1>
___TWIG2___

When multiple collections are used on the same page, the component can be assigned a name posts using the component alias and this is the variable name that becomes available to the page. The following collection is accessed using the posts variable instead.

[collection posts]
handle = "Blog\Post"
___TWIG0___
    <h1>___TWIG1___</h1>
___TWIG2___

Use the is empty or is not empty expression to check if a collection has at least one record available to display.

___TWIG0___
    ___TWIG1___
___TWIG2___

# How the Component Variable Works

The component variable (e.g. collection or your alias like posts) acts as a smart proxy that behaves differently depending on how you access it.

Accessing attributes returns the record field value, useful for section and global components.

___TWIG0___

Iterating directly returns the loaded records using the component's default query, useful for collection components.

___TWIG0___
    <h1>___TWIG1___</h1>
___TWIG2___

Calling a method starts a new database model query that can be chained and must end with get() or paginate().

___TWIG0___

When accessing the component variable as an attribute (e.g. posts.title) or as a query (e.g. posts.where(...)) the result will be different. Attributes return a stored value from the primary record, whereas methods initiate a new database query.

# Performing Queries

When accessing the component variable using a method it will switch to a database model query. For example, to only show entries that have a field color with the value blue use the where query method. Using the {% set %} Twig tag will assign the result to a new variable.

___TWIG0___

To show only posts that have a related author, you can use the whereRelation query method. To complete the query, proceed with the get() method. The following lists posts that are assigned to the author with a slug set to bella-vista.

___TWIG0___

# Accessing the Entry Type

To filter records by their entry type, perform a where() query using the content_group attribute. The following will list posts that use the entry type code featured_post.

___TWIG0___

# Paginating Records

You could also apply pagination to the collection with the paginate() method instead. The following will paginate the posts at 10 per page and displays the paginated navigation.

___TWIG0___

___TWIG1___

See the Pagination feature to learn more about paging records.

# Searching Records

To search records, use the searchWhere() method to perform a search query on a column's value. The following will search for records the supplied term and columns using a case insensitive query.

___TWIG0___

You may also use the searchWhereRelation() method to search related records, where the relation name is included in the method for querying the relationship existence.

___TWIG0___

In some cases, and for performance reasons, you may wish to eager load related records. Use the load method on the collection, passing the relation name. In the next example, the categories relation will be loaded with every post in the collection.

___TWIG0___

# Counting Records

Let's assume you want to display a list of blog categories and count the number of posts in each category. First, define the inverse relation on the category blueprint as posts.

posts:
    type: entries
    source: Blog\Post
    inverse: categories
    hidden: true

Then, call the withCount function on the collection component, followed by get to return a collection of category records. This will create an attribute on each category called post_count.

___TWIG0___
___TWIG1___
    <h5>___TWIG2___ (___TWIG3___ posts)</h5>
___TWIG4___

# See Also