首页 文章

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

提问于
浏览
1

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

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

3 回答

  • 1

    As of 1.0.3 我没有't believe there is a way to do this (can'看到源文件中的任何内容) .

    但是,您可以快速创建自己的“ReadOnly”字段,因为Nova可以很容易地添加更多字段类型 .

    我可能只是耐心等待 - 将字段添加到字段的功能可能是未来版本中的一项功能 .

    像这样的东西会很酷:

    Text::make('date_created')
        ->sortable()
        ->isReadOnly()
    

    要么

    Text::make('date_created')
        ->sortable()
        ->attributes(['readonly'])
    
  • 0

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

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

    用法示例:

    Text:: make('SomethingImportant')
        ->withMeta(['extraAttributes' => [
              'readonly' => true
        ]]),
    
  • 4

    由于 App\Laravel\Nova\Fields\Field 是可宏的,因此您可以轻松添加自己的方法,使其成为只读,e.x .

    App\Providers\NovaServiceProvider 中,您可以在 parent::boot() 调用后添加此功能

    \Laravel\Nova\Fields\Field::macro('readOnly', function(){
        $this->withMeta(['extraAttributes' => [
            'readonly' => true
        ]]);
    
        return $this;
    });
    

    然后你可以像这样链接它

    Text::make("UUID")->readOnly()->help('you can not edit this field');
    

相关问题