Skip to content

Relations

Default relations

By default, the OFFLINE\Boxes\Models\Box model has four relations defined:

TypeNames
attachOnefile, image
attachManyfiles, images

You can use these in your Box partials without any further changes.

Additional relations

You can use October's extend method to add additional relations to the default OFFLINE\Boxes\Models\Box model.

To add new relations to the Box model, use the following code in your app/Provider.php's boot method:

php
class Provider extends AppBase
{
    public function boot()
    {
        \OFFLINE\Boxes\Models\Box::extend(function (Box $box) {
            $box->attachOne['logo'] = File::class;
            $box->belongsToMany['persons'] = [
                \Some\Plugin\Models\Person::class,
                'table' => '...',
            ];

            // Example to add additional repeater item relations.
            $box->hasMany['my_custom_repeater_items'] = [
                \OFFLINE\Boxes\Models\RepeaterItem::class,
                'key' => 'parent_id',
                'delete' => true,
            ];
        });
    }
}

Eager loading

Since your Box is rendered inside a loop, you will experience an N+1 query problem sooner or later.

To prevent this, an eagerLoad property can be defined in a Box config. Defined relations will automatically be eager-loaded by the plugin.

To eager load the relations from the example above, this Box config could be used:

yaml
handle: eager-loading-example
eagerLoad:
- logo
- persons