翻译进度
15
分块数量
5
参与人数

模型关联

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。

除了我们之前讨论过的那些字段,Nova 还支持 Laravel 模型的关联关系。只要给资源添加一个关联关系,你就能体会到 Nova 的强大之处,因为你可以在资源详情页快速的查看并搜索该资源的关联模型:

Detail Screen Relationship

LaLaLaravel 翻译于 2个月前

一对一

这个 HasOne 对应一个 hasOne Eloquent 关系。例如,让我们假设一个 User 模型 hasOne Address 模型。我们可以将这种关系添加到我们的 User Nova 资源中,如下所示:

use Laravel\Nova\Fields\HasOne;

HasOne::make('Address')

就像其他类型的字段一样,关系字段将自动地 "snake case" 字段的显示名称,来确定底层关系的方法或属性。然而,你可以将它作为第二个参数传递给这个字段的 make 方法,来明确地指定关系方法的名称。如下所示:

HasOne::make('Dirección', 'address')
bai615 翻译于 2个月前

一对多

HasMany 字段对应 Eloquent 中的 hasMany 关系。假设一个 User 模型 hasMany Post 模型,那么我们可以给 Nova 的 User 资源添加如下关系:

use Laravel\Nova\Fields\HasMany;

HasMany::make('Posts')

一对多(逆向)

BelongsTo 字段对应 Eloquent 中的 belongsTo 关系。假设一个 Post 模型 belongsTo 一个 User 模型,那么我们可以给 Nova 的 Post 资源添加如下关系:

use Laravel\Nova\Fields\BelongsTo;

BelongsTo::make('User')
LaLaLaravel 翻译于 2个月前

标题属性

当创建或编辑包含 BelongsTo 字段的资源时,下拉框或搜索框里会显示该资源的「标题」。假如 User 资源的标题是 name 属性,那么 BelongsTo 选择菜单里显示的就是他的 name

Belongs To Title

如果要自定义资源的「titile」,可以在资源类里定义一个 title 属性:

public static $title = 'name';

此外,你也可以覆盖资源的 title 方法:

/**
 * 获取资源显示的值。
 *
 * @return string
 */
public function title()
{
    return $this->name;
}
LaLaLaravel 翻译于 2个月前

BelongsToMany

BelongsToMany 字段对应于模型的 belongsToMany 关联. 例如, 我们假定有一个 User 模型 belongsToMany Role 模型. 我们就可以像这样将它添加到 User Nova 资源中:

use Laravel\Nova\Fields\BelongsToMany;

BelongsToMany::make('Roles')

Pivot Fields

如果你的 belongsToMany 关联中间表还存着其他的关联字段 , 你也可以将它附到 BelongsToMany Nova 关联字段上. 一旦这些字段被附到该关联, 那他们将会在关联资源列表中展示.

Toiu 翻译于 2个月前

例如,我们假定我们的 User 模型 belongsToMany Role 模型. 在 role_user 中间表, 让我们想象一下 notes字段包含一些关联关系中简单的文本 我们可以使用 fields 方法把这个字段附加到 BelongsToMany 字段上:

BelongsToMany::make('Roles')
    ->fields(function () {
        return [
            Text::make('Notes'),
        ];
    });

当然, 这也可以在定义相反关联时使用. 所以, 如果我们在 User 资源中定义了一个BelongsToMany 字段 , 我们也可以在 Role 资源中定义反向关联:

BelongsToMany::make('Users')
    ->fields(function () {
        return [
            Text::make('Notes'),
        ];
    });

在两处分别定义这样的关联可能会让我们的代码重复, Nova 允许你通过传入一个可调用的对象给 fields 方法:

BelongsToMany::make('Users')->fields(new RoleUserFields)

上面的例子中, RoleUserFields 类可以只有一个 __invoke 方法, 并返回一个包含附加字段的数组:

<?php

namespace App\Nova;

use Laravel\Nova\Fields\Text;

class RoleUserFields
{
    /**
     *  获取关联模型的附加字段
     *
     * @return array
     */
    public function __invoke()
    {
        return [
            Text::make('Notes'),
        ];
    }
}
Toiu 翻译于 2个月前

Pivot Actions

通常, Nova actions 是基于资源操作的。 然而, 你也可以把 actions 绑定到 belongsToMany 字段 然后他们就可以在 pivot / 中间表记录上操作了。为了实现它,你可以在字段定义时使用 actions 链式方法:

BelongsToMany::make('Roles')
    ->actions(function () {
        return [
            new Actions\MarkAsActive,
        ];
    });

一旦 action 被绑定到这个字段,你就可以从父级资源屏幕上的关系索引中选择这个 action 并且执行它。

Actions

了解更多关于 Nova actions, 查看完成文档 action documentation.

JeremyKuang 翻译于 2个月前

标题属性

BelongsToMany 字段出现在资源创建和更新操作时,一个下拉选择菜单或搜索框会显示当前资源的 "title" 。例如: Role 资源可以使用 name 属性作为其标题。然后,当资源显示在 BelongsToMany 选择菜单时,这个属性也会显示。

Belongs To Many Title

为了自定义这个资源的 "title" ,你可以在类内定义一个 title 成员属性:

public static $title = 'name';

另外,你可以复写资源的 title 方法:

/**
 * 获取资源需要被显示的值
 * @return string
 */
public function title()
{
    return $this->name;
}
JeremyKuang 翻译于 2个月前

MorphMany

MorphMany 字段响应 morphMany Eloquent 关联。例如:我们假设
PostComment 模型存在一对多的多态关联。我们可以为 Post Nova 资源添加关联,如下:

use Laravel\Nova\Fields\MorphMany;

MorphMany::make('Comments')
JeremyKuang 翻译于 2个月前

MorphTo

The 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:

use Laravel\Nova\Post;
use Laravel\Nova\Video;
use Laravel\Nova\Fields\MorphTo;

MorphTo::make('Commentable')->types([
    Post::class,
    Video::class,
])

As you can see in the example above, the 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 screens:

Morph To Type

MorphTo Title Attributes

When a MorphTo field is shown on a resource creation / update screen, the title attributes of the available resources will automatically be displayed.

MorphToMany

The 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:

use Laravel\Nova\Fields\MorphToMany;

MorphToMany::make('Tags')

Pivot Fields

If your 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::make('Tags')
    ->fields(function () {
        return [
            Text::make('Notes'),
        ];
    });

Of course, it is likely we would also define this field on the inverse of the relationship. So, if we define the MorphToMany field on the Post resource, we would define it's inverse on the Tag resource:

MorphToMany::make('Posts')
    ->fields(function () {
        return [
            Text::make('Notes'),
        ];
    });

Since defining the field on both ends of the relationship can cause some code duplication, Nova allows you to pass an invokable object to the fields method:

MorphToMany::make('Users')->fields(new TaggableFields)

In this example, the TaggableFields class would be a simple, invokable class that returns the array of pivot fields:

<?php

namespace App\Nova;

use Laravel\Nova\Fields\Text;

class TaggableFields
{
    /**
     * Get the pivot fields for the relationship.
     *
     * @return array
     */
    public function __invoke()
    {
        return [
            Text::make('Notes'),
        ];
    }
}

Title Attributes

When a MorphToMany field is shown on a resource creation / update screen, 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:

Morph To Many Title

To customize the "title" attribute of a resource, you may define a title property on the resource class:

public static $title = 'name';

Alternatively, you may override the resource's title method:

/**
 * Get the value that should be displayed to represent the resource.
 *
 * @return string
 */
public function title()
{
    return $this->name;
}

Searchable Relations

By default, when the BelongsTo, BelongsToMany, MorphTo, and MorphToMany relationship fields are shown on a resource creation / update screen, 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:

Belongs To Search

To mark as relationship as searchable, chain the searchable method onto the field's definition:

BelongsTo::make('User')->searchable()

本文章首发在 Laravel China 社区
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

参与译者:5
讨论数量: 0
发起讨论


暂无话题~