试图从模型中获取数组值。 octobercms

时间:2018-02-21 01:34:44

标签: octobercms octobercms-plugins octobercms-backend

我无法在Model中检索数组的值。所以我的模型有中继器字段。现在我试图从另一个模型访问该转发器字段的值。

这就是我所做的......

已更新

WartaDataProfil 模型

中的

fields.yaml

fields:
visimisi:
    label: 'Visi Misi'
    ---
jdwlibdh:
    label: 'Jadwal'
    oc.commentPosition: ''
    prompt: 'add'
    maxItems: '7'
    span: full
    type: repeater
    form:
        fields:
            jeniskeb:
                label: 'Jenis'
                ---
            jamibdh:
                label: 'Jam'
                ---
            kategori:
                label: 'Kategori'
                type: dropdown
                options:
                    Umum/Raya: Umum/Raya
                    Komisi/Kategorial: Komisi/Kategorial
  

型号名称 WartaDataProfil

class WartaDataProfil extends Model
{
  use \October\Rain\Database\Traits\Validation;

  public $timestamps = false;

  public $rules = [
  ];   
  public $table = 'mismaiti_mywarta_profil';

  protected $jsonable = ['pdtjmt','jdwlibdh','komisi','bidang'];

  public $attachOne = [

      'logogereja' => 'System\Models\File',
      'logomywarta' => 'System\Models\File'

    ];
}

我想从 WartaDataProfil 上的'jeniskeb'字段中检索数据,以便在另一个模型上使用(假设模型是 WartaRutin )作为其中一个的下拉选项字段,所以我在 WartaRutin 模型

上写这些 WartaRutin 模型

上的

fields.yaml

tabs:
fields:        
    tanggal:
        label: Tanggal            
        mode: date
        format: 'd - F - Y'
        span: storm
        cssClass: col-sm-4 col-sm-push-0
        type: datepicker
        tab: 'Minggu Ini'                            
    kebum:
        label: ''            
        prompt: 'Tambah Data'
        maxItems: '3'            
        type: repeater
        tab: 'Minggu Ini'
        form:
            fields:
                jeniskeb:
                    label: 'Kebaktian'
                    ---
                    type: dropdown
                    options: jenisKeb
                khotbah:
                    label: Pengkhotbah
                    ---
                    type: text

这是 WartaRutin 模型(更新@HardikSatsiya解决方案)

class WartaRutin extends Model
{
  use \October\Rain\Database\Traits\Validation;

  use \October\Rain\Database\Traits\SoftDelete;

  protected $dates = ['deleted_at'];
  public $rules = [    ];    
  public $table = 'mismaiti_mywarta_rutin';
  protected $jsonable = ['temakebum','rankhot','kebum','kebkom','bngmbr'];

  public function jenisKeb(){
      $jdwlibdh = WartaDataProfil::pluck('jdwlibdh');

    $jenkebOptions= [''];
    foreach($jdwlibdh as $item)  {

        // json decoding 
        $decoded = json_decode($item, true);

        print(dd($decoded));

        // manually filtering
        if($decoded['kategori'] == 'Umum/Raya') {
            $jenkebOptions[$decoded['jeniskeb']] = $decoded['jeniskeb'];      
        }
    }

    return $jenkebOptions;
}   

@HardikSatasiya的解决方案返回错误

  

未定义索引:kategori

print(dd($ decoding))result

array:7 [▼
  0 => array:3 [▼
  "jeniskeb" => "Kebaktian Umum 1"
  "jamibdh" => "07.00 WIB"
  "kategori" => "Umum/Raya"
  ]
  1 => array:3 [▼
  "jeniskeb" => "Kebaktian Umum 2"
  "jamibdh" => "09.00 WIB"
  "kategori" => "Umum/Raya"
  ]
  2 => array:3 [▼
  "jeniskeb" => "Kebaktian Umum 3"
  "jamibdh" => "18.00 WIB"
  "kategori" => "Umum/Raya"
  ]
  3 => array:3 [▼
  "jeniskeb" => "Kebaktian Pemuda"
  "jamibdh" => "09.30 WIB"
  "kategori" => "Komisi/Kategorial"
  ]
  4 => array:3 [▼
  "jeniskeb" => "Kebaktian Remaja"
  "jamibdh" => "09.30 WIB"
  "kategori" => "Komisi/Kategorial"
  ]
  5 => array:3 [▼
  "jeniskeb" => "Kebaktian Tunas Remaja"
  "jamibdh" => "09.30 WIB"
  "kategori" => "Komisi/Kategorial"
  ]
  6 => array:3 [▼
  "jeniskeb" => "Kebaktian Anak"
  "jamibdh" => "09.30 WIB"
  "kategori" => "Komisi/Kategorial"
  ]
] 

1 个答案:

答案 0 :(得分:1)

正常方法无效,因为您的数据为repeater,因此数据库中的数据为JSON

  

您需要放入WartaRutin模型

的代码
public function jenisKeb(){

    $jdwlibdh = WartaDataProfil::pluck('jdwlibdh');

    $jenkebOptions= [''];
    foreach($jdwlibdh as $item)  {

        // json decoding 
        $decoded = json_decode($item, true);
        foreach($decoded as $innerItem)  {

            // manually filtering
            if($innerItem['kategori'] == 'Umum/Raya') {
                $jenkebOptions[$innerItem['jeniskeb']] = $innerItem['jeniskeb'];      
            }
        }
    }

    return $jenkebOptions;
}

这应该工作让我知道它是否有效。