Relationships are an important part of any web application. Most of your models will be related in some way to other models. Runway provides a way to manage those relationships within Statamic.
Runway provides a dedicated fieldtype to manage belongsTo
relationships within Statamic.
<?phpnamespace 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);}}
<?phpnamespace 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_idfield:type: belongs_todisplay: Authorresource: author
-handle: author_idfield:type: belongs_todisplay: Authorresource: author
You should make sure that the field handle matches the column name in the database.
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 }})
Written by {{ author:first_name }} {{ author:last_name }} ({{ author:location }})
Option | Description |
---|---|
mode |
Set the UI style for this field. Can be one of 'default' (Stack Selector), 'select' (Select Dropdown) or 'typeahead' (Typeahead Field). |
resource |
Specify the Runway Resource to be used for this field. |
relationship_name |
The name of the Eloquent Relationship this field should use. When left empty, Runway will attempt to guess it based on the field's handle. |
create |
By default you may create new models. Set to false to only allow selecting from existing models. |
with |
Specify any relationships you want to be eager loaded when this field is augmented. This option accepts an array of relationships. |
title_format |
Configure the title format used for displaying results in the fieldtype. You can use Antlers to pull in model data. |
Runway provides a dedicated fieldtype to manage hasMany
relationships within Statamic.
<?phpnamespace 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);}}
<?phpnamespace 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: postsfield:type: has_manydisplay: Postsresource: post
-handle: postsfield:type: has_manydisplay: Postsresource: post
You should ensure that the field handle matches the name of the Eloquent relationship in your model (the method name).
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>
<ul>{{ related_posts }}<li><a href="{{ url }}">{{ title }}</a></li>{{ /related_posts }}</ul>
Option | Description |
---|---|
mode |
Set the UI style for this field. Can be one of 'default' (Stack Selector), 'select' (Select Dropdown) or 'typeahead' (Typeahead Field). |
resource |
Specify the Runway Resource to be used for this field. |
relationship_name |
The name of the Eloquent Relationship this field should use. When left empty, Runway will attempt to guess it based on the field's handle. |
create |
By default you may create new models. Set to false to only allow selecting from existing models. |
with |
Specify any relationships you want to be eager loaded when this field is augmented. This option accepts an array of relationships. |
title_format |
Configure the title format used for displaying results in the fieldtype. You can use Antlers to pull in model data. |
reorderable |
Determines whether the models can be reordered. Defaults to false . |
order_column |
When 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. |
The Has Many fieldtype is also compatible with belongsToMany
relationships. You can use the Has Many fieldtype on both sides of the relationship.
<?phpnamespace 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);}}
<?phpnamespace 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: postsfield:type: has_manydisplay: Postsresource: post
-handle: postsfield:type: has_manydisplay: Postsresource: 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.
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.