Skip to main content

Belongs To

Runway provides a dedicated fieldtype to manage belongsTo relationships within Statamic.
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Post extends Model
{
    public function author(): BelongsTo
    {
        return $this->belongsTo(Author::class);
    }
}
-
  handle: author_id
  field:
    type: belongs_to
    display: Author
    resource: author
You should make sure that the field handle matches the column name in the database.

Templating

In Antlers, you can access any of the fields on the model. They’ll be augmented using the resource’s blueprint.
Written by {{ author:first_name }} {{ author:last_name }} ({{ author:location }})

Options

OptionDescription
modeSet the UI style for this field. Can be one of ‘default’ (Stack Selector), ‘select’ (Select Dropdown) or ‘typeahead’ (Typeahead Field).
resourceSpecify the Runway Resource to be used for this field.
relationship_nameThe name of the Eloquent Relationship this field should use. When left empty, Runway will attempt to guess it based on the field’s handle.
createBy default you may create new models. Set to false to only allow selecting from existing models.
withSpecify any relationships you want to be eager loaded when this field is augmented. This option accepts an array of relationships.
title_formatConfigure the title format used for displaying results in the fieldtype. You can use Antlers to pull in model data.
query-scopesAllows you to specify a query scope which should be applied when retrieving selectable models. You should specify the query scope’s handle, which is usually the name of the class in snake case. For example: MyAwesomeScope would be my_awesome_scope.

Has Many

Runway provides a dedicated fieldtype to manage hasMany relationships within Statamic.
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Author extends Model
{
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}
-
  handle: posts
  field:
    type: has_many
    display: Posts
    resource: post
You should ensure that the field handle matches the name of the Eloquent relationship in your model (the method name).

Templating

Loop through the models and do anything you want with the data.
<ul>
    {{ related_posts }}
        <li><a href="{{ url }}">{{ title }}</a></li>
    {{ /related_posts }}
</ul>

Options

OptionDescription
modeSet the UI style for this field. Can be one of ‘default’ (Stack Selector), ‘select’ (Select Dropdown) or ‘typeahead’ (Typeahead Field).
resourceSpecify the Runway Resource to be used for this field.
relationship_nameThe name of the Eloquent Relationship this field should use. When left empty, Runway will attempt to guess it based on the field’s handle.
createBy default you may create new models. Set to false to only allow selecting from existing models.
withSpecify any relationships you want to be eager loaded when this field is augmented. This option accepts an array of relationships.
title_formatConfigure the title format used for displaying results in the fieldtype. You can use Antlers to pull in model data.
reorderableDetermines whether the models can be reordered. Defaults to false.
order_columnWhen reordering is enabled, this determines which column should be used for storing the sort order. When the relationship uses a pivot table, the order column must exist on the pivot table.
query-scopesAllows you to specify a query scope which should be applied when retrieving selectable models. You should specify the query scope’s handle, which is usually the name of the class in snake case. For example: MyAwesomeScope would be my_awesome_scope.

Belongs To Many

The Has Many fieldtype is also compatible with belongsToMany relationships. You can use the Has Many fieldtype on both sides of the relationship.
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Category extends Model
{
    public function posts(): BelongsToMany
    {
        return $this->belongsToMany(Post::class);
    }
}
-
  handle: posts
  field:
    type: has_many
    display: Posts
    resource: post
You should ensure that the field handle matches the name of the Eloquent relationship in your model (the method name). For more information on templating with the Has Many fieldtype and the config options available, see the Has Many section.

Polymorphic Relationships

Runway doesn’t currently support Polymorphic relationships out of the box, since they can get pretty complicated. If you need it, please upvote this feature request.
I