Appearance
Relations
Default relations
By default, the OFFLINE\Boxes\Models\Box
model has four relations defined:
Type | Names |
---|---|
attachOne | file , image |
attachMany | files , images |
You can use these in your Box partials without any further changes.
Repeater Relations with File Upload Support Since v3.4
The Box
model has a default has-many relation called repeater_items
that references the built in \OFFLINE\Boxes\Models\RepeaterItem
model. This relation can be used for repeater fields.
The referenced model enables you to attach file uploads to each repeater item.
The RepeaterItem
model has the same default relations as the Box
model (see above, file
, image
, files
, images
).
yaml
name: Repeater Example
handle: repeater-example
form:
fields:
text:
label: Some text field
type: text
repeater_items: # Must be called repeater_items!
type: repeater
form:
fields:
some_field:
label: Some field
# File upload fields must be called
# file, files, image or images by default.
images:
label: Photos
type: fileupload
mode: image
Additional repeater relations
If you need multiple repeater field relations on a single Box, you can add additional relations for the OFFLINE\Boxes\Models\RepeaterItem
model yourself as described below.
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