Skip to content

Use sub-collections to manage HasMany relationships

A sub-collection is a regular collection but nested inside another one. It's useful to group related resources together in composition scenarios (meaning when a child item cannot exist without its parent).

Example

Let's imagine that we want to display meaningful figures (KPI) on a project page. In this scenario, the Figure collection can and should be defined as a sub-collection of the Project collection.

Here's how it would be displayed in Ozu Dashboard: Sub-collection example

Configuration

Sub-collections are configured in the same way as regular collections, but you must not declare them in the collections array of your ozu-client.php configuration file.

Instead, you declare them in the parent collection's class, like this:

php
namespace App\Models;

class Project extends Model
{
    use IsOzuModel;

    public static function configureOzuCollection(OzuCollectionConfig $config): OzuCollectionConfig
    {
        return $config
            ->setLabel('Projects')
            // ...
            ->addSubCollection(Figure::class);
    }
    
    // ...
}

And that's it! From there, Ozu will automatically discover and register the sub-collection and handle the BelongsTo relationship in the CMS part of Ozu Dashboard. Of course, you may need in your project to define the relationship in the model class as well.