Nova ships with a variety of fields out of the box, including fields for text inputs, booleans, dates, file uploads, Markdown, and more.
HasOne
field corresponds to a hasOne
Eloquent relationship. For example, let’s assume a User
model hasOne
Address
model. We may add the relationship to our User
Nova resource like so:
make
method:
HasOne
relationship field can be transformed into an “has one of many” Eloquent relationship using the ofMany
method. For example, let’s assume a User
model hasMany
Post
models. We may add the “has one of many” relationship to our User
Nova resource like so:
HasMany
field corresponds to a hasMany
Eloquent relationship. For example, let’s assume a User
model hasMany
Post
models. We may add the relationship to our User
Nova resource like so:
HasMany
relationships, make sure to use the plural form of the relationship so Nova can infer the correct singular resource name:HasOneThrough
field corresponds to a hasOneThrough
Eloquent relationship. For example, let’s assume a Mechanic
model has one Car
, and each Car
may have one Owner
. While the Mechanic
and the Owner
have no direct connection, the Mechanic
can access the Owner
through the Car
itself. You can display this relationship by adding it to your Nova resource:
HasManyThrough
field corresponds to a hasManyThrough
Eloquent relationship. For example, a Country
model might have many Post
models through an intermediate User
model. In this example, you could easily gather all blog posts for a given country. To display this relationship within Nova, you may add it to your Nova resource:
BelongsTo
field corresponds to a belongsTo
Eloquent relationship. For example, let’s assume a Post
model belongsTo
a User
model. We may add the relationship to our Post
Nova resource like so:
make
method, which define the name of the relationship and the underlying Nova resource class:BelongsTo
link when viewing the index or detail views, Nova will show a small card allowing you to “take a peek” at the linked relation:
BelongsTo
RelationshipsnoPeeking
helper on your BelongsTo
field:
peekable
method to determine whether the user should be allowed to peek at the relation:
BelongsTo
relationship to be nullable
, you may simply chain the nullable
method onto the field’s definition:
BelongsTo
field is shown on a resource creation / update page, a drop-down selection menu or search menu will display the “title” of the resource. For example, a User
resource may use the name
attribute as its title. Then, when the resource is shown in a BelongsTo
selection menu, that attribute will be displayed:
title
property or title
method on the resource class:
dontReorderAssociatables
method, you can disable this behavior so that the resources as sorted based on the ordering specified by the relatable query:
BelongsTo
field will allow users to select soft-deleted models; however, this can be disabled using the withoutTrashed
method:
BelongsToMany
field corresponds to a belongsToMany
Eloquent relationship. For example, let’s assume a User
model belongsToMany
Role
models:
User
Nova resource like so:
make
method:
belongsToMany
relationship interacts with additional “pivot” fields that are stored on the intermediate table of the many-to-many relationship, you may also attach those to your BelongsToMany
Nova relationship. Once these fields are attached to the relationship field, and the relationship has been defined on both of the related models / resources, they will be displayed on the related resource index.
For example, let’s assume our User
model belongsToMany
Role
models. On our role_user
intermediate table, let’s imagine we have a notes
field that contains some simple text notes about the relationship. We can attach this pivot field to the BelongsToMany
field using the fields
method:
BelongsToMany
field on the User
resource, we would define its inverse on the Role
resource:
withPivot
method:
https://laravel.com/docs/eloquent-relationships#retrieving-intermediate-table-columnsfields
method:
RoleUserFields
class would be a simple, invokable class that returns the array of pivot fields:
belongsToMany
relationship field:
belongsToMany
fields so that they can operate on pivot / intermediate table records. To accomplish this, you may chain the actions
method onto your field’s definition:
BelongsToMany
field is shown on a resource creation / update page, a drop-down selection menu or search menu will display the “title” of the resource. For example, a Role
resource may use the name
attribute as its title. Then, when the resource is shown in a BelongsToMany
selection menu, that attribute will be displayed:
title
property or title
method on the resource class:
dontReorderAttachables
method, you can disable this behavior so that the resources as sorted based on the ordering specified by the relatable query:
id
column is available by using the withPivot
method when defining the relationship on your Eloquent model. In this example, let’s imagine that a User
may purchase a Book
one or more times:
allowDuplicateRelations
method:
MorphOne
field corresponds to a morphOne
Eloquent relationship. For example, let’s assume a Post
has a one-to-one polymorphic relationship with the Image
model. We may add the relationship to our Post
Nova resource like so:
MorphOne
relationship field can be transformed into a “morph one of many” Eloquent relationship using the ofMany
method. For example, let’s assume a Post
has a one-to-many polymorphic relationship with the Comment
model. We may add the relationship to our Post
Nova resource like so:
MorphMany
field corresponds to a morphMany
Eloquent relationship. For example, let’s assume a Post
has a one-to-many polymorphic relationship with the Comment
model. We may add the relationship to our Post
Nova resource like so:
MorphTo
field corresponds to a morphTo
Eloquent relationship. For example, let’s assume a Comment
model has a polymorphic relationship with both the Post
and Video
models. We may add the relationship to our Comment
Nova resource like so:
types
method is used to instruct the MorphTo
field what types of resources it may be associated with. Nova will use this information to populate the MorphTo
field’s type selection menu on the creation and update pages:
MorphTo
field is shown on a resource creation / update page, the title attributes of the available resources will automatically be displayed.MorphTo
RelationshipsMorphTo
relationship to be nullable
, chain the nullable
method onto the field’s definition:
MorphTo
RelationshipsMorphTo
link when viewing the index or detail views, Nova will show a small card allowing you to “take a peek” at the linked relation:
MorphTo
RelationshipsnoPeeking
helper on your MorphTo
field:
peekable
method to determine whether the user should be allowed to peek at the relation:
MorphTo
RelationshipsMorphTo
field, in addition to setting the field’s initial value using the default
method, you also need to specify the class name of the resource to be used. You may accomplish this via the defaultResource
method:
MorphToMany
field corresponds to a morphToMany
Eloquent relationship. For example, let’s assume a Post
has a many-to-many polymorphic relationship with the Tag
model. We may add the relationship to our Post
Nova resource like so:
morphToMany
relationship interacts with additional “pivot” fields that are stored on the intermediate table of the many-to-many relationship, you may also attach those to your MorphToMany
Nova relationship. Once these fields are attached to the relationship field, they will be displayed on the related resource index.
For example, on our taggables
intermediate table, let’s imagine we have a notes
field that contains some simple text notes about the relationship. We can attach this pivot field to the MorphToMany
field using the fields
method:
MorphToMany
field on the Post
resource, we would define it’s inverse on the Tag
resource:
withPivot
method:
https://laravel.com/docs/eloquent-relationships#retrieving-intermediate-table-columnsfields
method:
TaggableFields
class would be a simple, invokable class that returns the array of pivot fields:
MorphToMany
field is shown on a resource creation / update page, a drop-down selection menu or search menu will display the “title” of the resource. For example, a Tag
resource may use the name
attribute as its title. Then, when the resource is shown in a MorphToMany
selection menu, that attribute will be displayed:
title
property or title
method on the resource class:
BelongsToMany
, HasMany
, and MorphToMany
relationship fields are shown on the resource detail page. However, this can quickly become cumbersome if a resource has many performance-intensive relationships which cause the page to be slow.
For this reason, Nova allows you to mark as relationship as collapsable
. When a relationship is collapsable, users may collapse some of the relations for a given resource and Nova will remember their preferences on subsequent page loads. Collapsed relationships are not retrieved from the database until the relationship is expanded in Nova’s user interface:
collapsedByDefault
method:
BelongsTo
, MorphTo
, BelongsToMany
and MorphToMany
relationship fields are shown on a resource creation / update page, a simple drop-down selection menu will be displayed. However, this can quickly become cumbersome if a resource model has many records. For example, imagine a drop-down selection menu populated with over 10,000 users!
Instead of displaying a drop-down selection menu, you may mark your relationships as searchable
. When a relationship is marked as searchable
, a beautiful search input control will be displayed instead:
searchable
, chain the searchable
method onto the field’s definition. If you would like to conditionally determine if a field should be searchable, you may pass a closure to the searchable
method:
withSubtitles
method when defining the field:
relatableQueryUsing
method:
relatableQueryUsing
method may also prove useful when you need to adjust the query based on the value of another field:
relatableSearchResults
property on the class of the resource that you are searching for:
BelongsTo
or MorphTo
relationship fields are shown on a resource create or update page, you may create the related resource inline via a modal window without leaving the creation / update page:
showCreateRelationButton
method when defining the relationship field:
showCreateRelationButton
method to conditionally determine if inline resource creation should be enabled:
showCreateRelationButton
when defining a BelongsToMany
or MorphToMany
relationship:
hideCreateRelationButton
method when defining the relationship field:
modalSize
method: