| description |
|---|
A collection class for managing CMS items, providing methods for filtering, ordering, pagination, and searching. |
Creates a new collection instance.
- $cms (
ElementsCMS): The CMS instance. - $path (
string): Path to the collection (folder). - $options (
array): Additional options for the collection.
Add relations to be eager-loaded for all items in the collection.
- $relations (
string ...): One or more relation names. - Returns:
$this(for chaining)
Filter items in the collection by key/value pairs.
- $criteria (
array): Associative array of criteria (e.g.,['status' => 'published']) - Returns:
$this(for chaining)
Filter items in the collection using a custom callback.
- $callback (
callable): Function that receives an item and returnstrueto keep,falseto exclude. - Returns:
$this(for chaining)
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)
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 the collection.
- $page (
int): Page number (default:1) - $perPage (
int): Items per page (default:10) - Returns:
array— Paginated results, typically includingitems,total,page,per_page, etc.
Returns the underlying Laravel collection of items.
- Returns:
Illuminate\Support\CollectionofElementsCMSItemobjects.
Find an item in the collection by its slug.
- $slug (
string): The slug to search for. - Returns:
ElementsCMSItem|null
Search the collection items by query string.
- $query (
string): The search term. - Returns:
array— Array of matchingElementsCMSItemobjects.
$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');