简体   繁体   English

Laravel Nova - 字段是否可以依赖父资源字段与 HasOne 关系

[英]Laravel Nova - Can a field dependsOn parent resource field with-in a HasOne relationship

The parent resource has these fields父资源具有这些字段

    Select::make('Currency')->options(...),
    HasOne::make('Child', 'child', 'App\Nova\ChildResource')->required(),

as I've added HasOne->required, the child resource field are displayed inline (on the same page) while I create (Add new) parent resource.当我添加 HasOne->required 时,子资源字段在我创建(添加新的)父资源时内联显示(在同一页面上)。 The child fields are as follows:子字段如下:

    Text::make('Some Field')
       ->dependsOn(['???'], function() {})   //it depends upon Currency Field in the parent resource

I want to know if this is possible?我想知道这是否可能? if yes then what attribute I should mention instead of???如果是,那么我应该提到什么属性而不是??? in dependsOn?在取决于?

I've tried using 'currency', but it does not work...我试过使用“货币”,但它不起作用......

The other end of the HasOne relationship is a Child's parent function that returns a belongsTo. HasOne 关系的另一端是返回 belongsTo 的 Child 的父级 function。

function parent()
{
    return $this->belongsTo(Parent::class);
}

add the relationship in nova在 nova 中添加关系

BelongsTo::make('Parent'),

and then you can use depends然后你可以使用 depends

Text::make('Some Field')->dependsOn(['parent'], 
    function (Text $field, NovaRequest $request, FormData $formData) {
        $model = $request->findModel();
        if ($model && $model->parent && $model->parent->currency === 'USD') 
        {
            $field->readonly(true);
        }
    }
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel nova 4 中的依赖字段,dependsOn() 方法, - dependent field in laravel nova 4, dependsOn() method, Laravel Nova | 从Laravel Nova资源存储布尔字段时如何传递默认值 - Laravel Nova | How can i pass default value while storing Boolean field from Laravel nova resource Laravel Nova使用belongsTo保存资源字段 - Laravel nova save Resource field with belongsTo Laravel Nova HasOne Relationship视图并在单个面板中编辑2个模型 - Laravel Nova HasOne Relationship view and edit 2 models in single panel 以一种形式在 laravel nova 中添加来自关系的字段文本 - Add field text from relationship in laravel nova in ONE form Laravel 9 Nova 4 与自定义 Pivot 的关系表和字段名称 - Laravel 9 Nova 4 Relationship with custom Pivot Table and field names Laravel Nova - 基于与另一个下拉列表的关系加载下拉字段 - Laravel Nova - Load dropdown field based on relationship with of another dropdown Laravel nova 文件字段 | 如何在存储文件之前获取资源 ID - Laravel nova File Field | How can i get resource id before storing file Laravel Nova - 在 ID 字段中显示资源名称而不是实际 ID - Laravel Nova - Show the name of the resource in the ID field instead of the actual ID 限制在 BelongsTo select 字段中为 Laravel Nova 资源返回的选项数量 - Limit the number of options returned in the BelongsTo select field for a Laravel Nova resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM