Skip to content

Models are Collections

A Collection is a Model that can be listed and edited in the CMS section of the Ozu dashboard.

Declare a Collection

As seen before in the Configure Your Project section, you must adapt your migration, and add the IsOzuModel trait to your model: this will add some required columns / attributes, and allow Ozu to know about your model and its configuration.

php
use Code16\OzuClient\Eloquent\IsOzuModel;
use Code16\OzuClient\OzuCms\OzuCollectionConfig;
use Code16\OzuClient\OzuCms\OzuCollectionListConfig;
use Code16\OzuClient\OzuCms\OzuCollectionFormConfig;

class Project extends Model
{
    use IsOzuModel;

    public static function configureOzuCollection(OzuCollectionConfig $config): OzuCollectionConfig
    {
        return $config;
    }

    public static function configureOzuCollectionList(OzuCollectionListConfig $config): OzuCollectionListConfig
    {
        return $config;
    }

    public static function configureOzuCollectionForm(OzuCollectionFormConfig $config): OzuCollectionFormConfig
    {
        return $config;
    }
}

Handle relationships

Models declared as Ozu collections have an important limitation regarding relationships: only BelongsTo / HasMany relationships are supported, and each model may define only a single relationship, which must rely on a parent_id column. For example:

php
class Project extends Model
{
    use IsOzuModel;

    public function collaborators(): HasMany
    {
        return $this->hasMany(Collaborator::class, 'parent_id')
            ->orderBy('order');
    }
    
    // ...
}
php
class Collaborator extends Model
{
    use IsOzuModel;

    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class, 'parent_id');
    }
    
    // ...
}

This might seem limiting, but as we'll see below this does not apply to attached Media (visuals, documents...).

TIP

In Ozu Dashboard, this kind of relationship is usually handled by a Select field in the "child" collection form (see declareBelongsToField() in the Forms configuration section, You may also declare the child as a sub-collection, which is particularly useful for composition scenarios (see the related documentation).

Attach Media

If you want to attach media (say: visuals) to your Models, leverage the Code16\OzuClient\Eloquent\Media model via a MorphOne or a MorphMany relation:

php
class Project extends Model
{
    use IsOzuModel;
    
    public function visuals(): MorphMany
    {
        return $this->morphMany(Media::class, 'model')
            ->withAttributes(['model_key' => 'visuals'])
            ->orderBy('order');
    }

    public function ogImage(): MorphOne
    {
        return $this->morphOne(Media::class, 'model')
            ->withAttributes(['model_key' => 'ogImage']);
    }
    
    // ...
}

TIP

Every Collection comes with a default cover media field, no need to redefine it.

To learn more about Media, and especially about how to manipulate images, check the Handling Media section.

Configure Ozu Dashboard

Next, you need to configure how your collection should appear and be managed in the CMS section of the Ozu dashboard.

General configuration: label, icon, publication state, permissions...

This part is managed by the static configureOzuCollection() method, documented here.

This part is managed by the static configureOzuCollectionList() method, documented here.

Configure the form view: fields

This part is managed by the static configureOzuCollectionForm() method, documented here.

Sync your project configuration with Ozu

Run php artisan ozu:configure to sync your project configuration (for all your declared Collections) with Ozu, allowing to list and edit them from the CMS section of the Ozu dashboard. This operation will only sync the configuration and will not touch the data.