LogoLogo
  • Introduction
  • Early Access
  • System Requirements
  • Elements FAQ
  • Purchasing & Licensing FAQ
    • License Types
  • Why Elements?
    • Static Website Benefits
    • Build a Digital Garden
  • Community Forum
  • Marketplace
  • Elements App
    • Getting Started
    • Keyboard Shortcuts
    • Migrations
      • Migrating from Blocs
      • Migrating from Classic
      • Migrating from Stacks
    • Design System
    • Editor
      • Apple Intelligence
      • Component Inspector
      • Dark Mode
      • Node Browser
      • Page Manager
      • Preview
      • Resources
      • Responsive Breakpoints
      • Workspaces
    • Components
      • Built-in Components
        • Accordion
        • Background
        • CMS
          • Helpers
          • Available Classes
            • ElementsCMS.php
            • ElementsCMSCollection
            • ElementsCMSItem.php
          • Frontmatter
          • Markdown
          • Online Editor
          • Pretty URLs
          • Template Data
        • Button
        • Container
        • Divider
        • Dropdown
        • Flex
        • Filter
        • Filter Tags
        • Form
        • Gallery
        • Grid
        • Image
        • Menu
        • Modal
        • Modal Close
        • Reveal
        • Image Slider
        • SVG
        • Text
        • Top Pages
        • Typography
        • Video
      • Common Controls
        • Link
        • Layout
        • Sizing
        • Spacing
        • Transitions
        • Effects
        • Filters
        • Transforms
        • Borders
        • Advanced
      • Custom Components
    • Globals
    • Templates
    • Project Settings
      • General
      • Web Icons
      • Advanced
      • Template
      • Publishing
    • Theme Studio
      • Theme
      • Colors
      • Screens
      • Font Family
      • Font Size
      • Spacing
      • Shadows
      • Border Width
      • Border Radius
      • Typography
    • How to
      • Add an Icon Inside a Button
      • Adjust Smooth Scroll Speed
      • Apply Anchor Scroll Padding
      • Add Snow to your Website
      • Build a Sticky Menu
      • Center Align Components
      • Create a Card
      • Site Banner with Text
      • Make a two column layout
    • Resources
    • Troubleshooting
    • SEO
      • Robots.txt
    • Accessibility
    • Advanced
      • URL Scheme
  • Elements Language
    • Introduction
      • Getting Started
      • Component Styling
    • Element Pack
      • info.json
      • Components
        • info.json
        • Icons
        • Properties.json
          • Grouping Controls
          • Default Values
          • General Structure
            • Title
            • ID
            • Format
            • Visible
            • Enabled
            • Responsive
          • UI Controls
            • Divider
            • Heading
            • Image
            • Information
            • Link
            • Number
            • Resource
            • Segmented
            • Select
            • Slider
            • Switch
            • Text
            • Text Area
            • Theme Border Width
            • Theme Border Radius
            • Theme Color
            • Theme Font
            • Theme Spacing
            • Theme Shadow
            • Theme Text Style
            • Theme Typography
        • Templates
          • Portal
          • Backend
          • Conditional Statements
          • Regular Expressions
          • Looping
          • Includes
          • Image Resources
          • HTML
            • Anchor
            • Raw
            • Editable Content
            • Dropzones
            • Inline templates
          • CSS
          • JavaScript
        • Assets
        • Hooks.js
          • Common use cases
          • Passing data to templates
          • Working with Collections
          • Working with UI Controls
          • Working with Resources
          • Available Functions
            • rw.addAnchor
            • rw.getBreakpoints
            • rw.getBreakpointNames
            • rw.getResponsiveValues
            • rw.resizeResource
            • rw.setProps
            • rw.setRootElement
          • Available Data
            • rw.collections
            • rw.component
            • rw.node
            • rw.pages
            • rw.project
            • rw.props
      • Collections
        • Data collections in Hooks.js
        • Accessing Data in Templates
        • Collections in properties.json
      • Shared Files
        • Assets
        • Templates
      • Themes
    • Troubleshooting
  • Elements Cloud
    • Getting Started
    • Troubleshooting
  • Elements Marketplace
    • Getting Started
    • Add-on Guidelines
    • Marketplace API
  • Branding
    • Logotype
    • Logotype Animated
    • Icon
  • Legal
    • Subscription Terms of Service
    • Cloud Terms of Service
    • Frameworks
Powered by GitBook

We are Realmac Software. We make nice things.

On this page
  • Constructor
  • Instance Methods
  • Usage Example

Was this helpful?

Edit on GitHub
Export as PDF
  1. Elements App
  2. Components
  3. Built-in Components
  4. CMS
  5. Available Classes

ElementsCMSCollection

A collection class for managing CMS items, providing methods for filtering, ordering, pagination, and searching.

Constructor

__construct(ElementsCMS $cms, string $path, array $options = [])

Creates a new collection instance.

  • $cms (ElementsCMS): The CMS instance.

  • $path (string): Path to the collection (folder).

  • $options (array): Additional options for the collection.


Instance Methods

with(string ...$relations): self

Add relations to be eager-loaded for all items in the collection.

  • $relations (string ...): One or more relation names.

  • Returns: $this (for chaining)

filter(array $criteria = []): self

Filter items in the collection by key/value pairs.

  • $criteria (array): Associative array of criteria (e.g., ['status' => 'published'])

  • Returns: $this (for chaining)

filterWith(callable $callback): self

Filter items in the collection using a custom callback.

  • $callback (callable): Function that receives an item and returns true to keep, false to exclude.

  • Returns: $this (for chaining)

orderBy(string $field, string $direction = 'desc'): self

Order the collection by a specified field.

  • $field (string): Field name to order by.

  • $direction (string): 'asc' or 'desc' (default: 'desc')

  • Returns: $this (for chaining)

whereDate(string $field, string $operator, string $date): self

Filter items by date field using a comparison operator.

  • $field (string): Date field to compare (e.g., 'published_date')

  • $operator (string): Comparison operator (e.g., '>=', '==', '<')

  • $date (string): Date value for comparison.

  • Returns: $this (for chaining)

paginate(int $page = 1, int $perPage = 10): array

Paginate the collection.

  • $page (int): Page number (default: 1)

  • $perPage (int): Items per page (default: 10)

  • Returns: array — Paginated results, typically including items, total, page, per_page, etc.

getCollection(): LaravelCollection

Returns the underlying Laravel collection of items.

  • Returns: Illuminate\Support\Collection of ElementsCMSItem objects.

findBySlug(string $slug): ?ElementsCMSItem

Find an item in the collection by its slug.

  • $slug (string): The slug to search for.

  • Returns: ElementsCMSItem|null

search(string $query): array

Search the collection items by query string.

  • $query (string): The search term.

  • Returns: array — Array of matching ElementsCMSItem objects.


Usage Example

$collection = new ElementsCMSCollection($cms, 'posts', [
    'expectDates' => true
]);

// Filter, order, and paginate items
$results = $collection
    ->filter(['status' => 'published'])
    ->orderBy('published_date', 'desc')
    ->paginate(1, 10);

// Find a specific item by slug
$item = $collection->findBySlug('hello-world');

// Search items
$matches = $collection->search('static site generator');
PreviousElementsCMS.phpNextElementsCMSItem.php

Last updated 1 day ago

Was this helpful?