Skip to content

Collections

Collections (sometimes also referenced as "globals" or "sections") are containers that hold groups of related blocks. They are a great way to organize and manage global content without building a custom module.

Creating a collection

  1. (optional) Create a new blueprint for your collection. (See Creating a custom blueprint for more information on how to create a blueprint.).

    TIP

    Be sure to mark the blueprint as hidden if you don't want it to be selectable as a top-level block outside of the collection.

  2. Create a new collection in the control panel and select the blueprint as the type. Give the collection a human-readable label and a unique handle to reference it in your templates.

  3. Start adding entries to your collection 😃

Accessing collections in your templates

You can access the items of a collection in your templates (or anywhere else) by using the collection helper.

-> Read more about Queriying collections in the frontend documentation.

Routeable collection blocks

Collection blocks can be made routeable by enabling the routeable option in the collection blueprint method. This will automatically create routes for each entry in the collection.

php
Blueprint::make('custom.products')
    ->label('Produkte')
    ->collection(
        routeable: true,
    )

Frontend template

When visiting the URL of a routeable block, our controller will try to render a blade template that matches following naming convention: collections.{handle}.

Example

If you have a collection with the handle products, you should create a blade template at theme/views/collections/products.blade.php.

You can access the block using the $block variable in your template just like you would with a regular block.

-> Read more about Accessing field data in the frontend documentation.

Custom ordering

You can define a custom base query for your collection by passing a closure to the baseQuery parameter. This is useful if you want to apply a specific order in the control panel.

php
Blueprint::make('custom.products')
    ->label('Produkte')
    ->collection(
        routeable: true,
        baseQuery: fn (Builder $query) => $query->reorder()->latest(),
    )

Custom ordering

When defining a custom base query, make sure to always call reorder() before applying any other ordering methods. This ensures that the default "order by position" is removed. (Collections with a custom base query can't be reordered in the control panel.)

-> Read more about Blueprints in the data modeling documentation.

Relationships between collections

You can define relationships between collections by using the relationship method on a select, multi-select, radio group or checkbox group field.

Here's an example of a select field on a Product blueprint that is related to a Product Categories collection:

php
Field::select('category', 'Kategorie')->relationship('product_categories')

-> Read more about relationships in the Blueprint documentation.