首页 > 解决方案 > 筛选字段时出现“从空值创建默认对象”

问题描述

我正在尝试根据以特定字符串开头的用户名动态更改字段标签

public function filterFields($fields,$context = null)
{
      $nager = WartaDataProfil::where('backend_users_id',$this->getBackendUserId())->pluck('nager');        

      if(Str::startsWith($nager[0],'GP')){            

        $fields->wil->label = "Sektor";
      }

}

我有错误

从空值创建默认对象

从线

$fields->wil->label = "Sektor";

当我dd($fields->wil->label);得到在 yaml 文件中定义的默认字符串时。

字段.yaml

fields:
nowarta:
    label: 'No. Warta'
    ...
tanggal:
    label: 'Tanggal'
    ...
batasno:
    label: ''
    ...
    type: section           
tabs:
fields:
    renung:
        label: 'Renungan Mingguan'            
        span: full
        type: section 
        tab: 'Renungan/Artikel'           
    renungan:
        label: ''            
        ...  
        type: repeater
        ...
        minItems: '1'
        maxItems: '1'        
        form:
            fields:
                judul:
                    label: 'Judul'                        
                    ...
                bacaan:
                    label: 'Bahan'
                    ...

                penulis:
                    label: 'Penulis'                        
                    ...                      
                isi:
                    label: Renungan                        
                    ...
    keb:
        label: 'Kebaktian Umum'
        span: full
        type: section  
        tab: 'Kebaktian'                                
    kebakti:
        label: ''            
        span: full
        type: partial
        path: field_kebum
        tab: 'Kebaktian' 
    kom: 
        label: 'Kebaktian Komisi'
        span: left
        type: section  
        tab: 'Kebaktian'
    wil: <<<   THIS FIELD <<<<<<<<<<<<<<<<<
        label: 'Persekutuan Wilayah'            
        span: right
        type: section
        tab: 'Kebaktian'                  
    Komisi:
        label: ''            
        span: left
        type: partial
        path: field_kebkom
        tab: 'Kebaktian'       
    pwil:
        label: ''                     
        span: right
        type: partial
        path: field_pwilayah
        tab: 'Kebaktian'      

希望这有助于解决我的问题..因为我在其他模型中使用相同的 filterField 并且它工作正常..

标签: laravel-5octobercmsoctobercms-backend

解决方案


推荐阅读