如何将Laravel Nova字段设置为只读或受保护?

Mat*_*att 4 laravel-nova

在Laravel Nova(v1.0.3)中,有几种方法可以对资源字段的可见性进行细粒度控制(canSee,showOnDetail等).我找不到任何控制字段是否可编辑的方法.如何显示字段,但阻止用户编辑它(使其只读)?

例如,我想显示"Created At"字段,但我不希望用户能够更改它.

Mat*_*att 10

此功能已在v1.1.4(2018年10月1日)中添加.

  • 允许在text和textarea字段上设置任何属性

用法示例:

Text:: make('SomethingImportant')
    ->withMeta(['extraAttributes' => [
          'readonly' => true
    ]]),
Run Code Online (Sandbox Code Playgroud)

  • 请注意,在使用此解决方案时,它唯一要做的就是将该输入设置为只读,但它根本不会触及用户权限.因此,如果您在浏览器开发工具中删除了readonly属性并将不同的值设置为输入,那么它将保存新值.因此,如果您将此表单公开给您无法信任的用户,那么这可能很危险.但是,您可以在Laravel中保护用户观察器中的输入. (3认同)

ber*_*rdh 9

从 Nova 开始,>2.0您可以使用带有回调的 readonly 方法并检查资源:

Text::make("Read Only on Update")
    ->readonly(function() {
        return $this->resource->id ? true : false;
    }),
Run Code Online (Sandbox Code Playgroud)

甚至更好:

Text::make("Read Only on Update")
    ->readonly(function() {
        return $this->resource->exists;
    }),
Run Code Online (Sandbox Code Playgroud)


小智 5

As of v2.0.1, readonly() is native and accepts a callback, closure or boolean and can simply be called as:

Text::make('Name')->readonly(true)
Run Code Online (Sandbox Code Playgroud)

This may have been added prior to this version but the changelog does not specify if this is the case.

Nova v2.0 documentation