试图从模型中获取数组值。十月公关

trying to get array value from models. octobercms

我在检索模型中的数组值时遇到问题。所以我有一个有中继场的模型。现在我正在尝试从另一个模型访问该转发器字段的值。

这就是我所做的..

已更新

fields.yaml WartaDataProfil 模型

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

the model name 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 模型

fields.yaml WartaRutin 模特

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 return 错误的解决方案

Undefined index: kategori

打印(dd($解码))结果

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"
  ]
] 

常规方法将不起作用,因为您的数据是 repeater,所以它在数据库中将是 JSON

code you need to put inside WartaRutin model

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;
}

这应该可以让我知道它是否有效。