i am having trouble retrieve value of an array in Model. so i have model which has repeater field. now i am trying to access the value of that repeater field from another model.
this is what i have done..
UPDATED
fields.yaml in WartaDataProfil model
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'
];
}
i want to retrieve data from 'jeniskeb' field on WartaDataProfil to be use on another model(let say the model is WartaRutin) as dropdown options on one of the field, so i write these on WartaRutin model
fields.yaml on WartaRutin model
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
and this is WartaRutin model (Updated with solution of @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;
}
Solution of @HardikSatasiya return an error
Undefined index: kategori
print(dd($decoded)) 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"
]
]
Normal approach will not work as your data is repeater
so it will be JSON
in database.
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;
}
this should work let me know if it works or not.