This is a function to get file from two queries. When the file not exist in query $pegawai->simpegFilePegawai(), it will continue to search in query $pegawai->skpdSimpegFilePegawai().
I expected the result is the object with "file_id":1729467
. You can see in debug result.
However as we can see in debug result, this function returned another result which is empty array.
I don't understand, why my function returned two result?. Can you explain why, and how should i do to solve this.
CODE:
public static function getFileSerdikTerakhir(Pegawai $pegawai, $stagging = false) {
$query = $pegawai->riwayatSertifikat();
if ($stagging) {
$query = $pegawai->skpdRiwayatSertifikat();
}
$serdik = $query->where('nama_sertifikat', 'ilike', "%sertifikat pendidik%")
->orderByDesc('tanggal_sertifikat')
->get();
$info = [];
foreach ($serdik as $key => $item) {
$query = $pegawai->simpegFilePegawai();
if ($stagging) {
$query = $pegawai->skpdSimpegFilePegawai();
}
$info = $query->where('file_lokasi', 'ilike', "%[{$item->sertifikat_id}] Sertifikat%")->first();
Log::debug('staging');
Log::debug($stagging);
Log::debug('end staging');
Log::debug('info foreach');
Log::debug($info);
Log::debug('end info foreach');
if ($info) break;
}
if (empty($info) && ! $stagging) {
Log::debug('empty info');
SyaratPengajuan::getFileSerdikTerakhir($pegawai, true);
}
Log::debug('----return info----');
Log::debug($info);
Log::debug('----end return info----');
return $info;
}
DEBUG RESULT:
[2021-04-16 08:19:46] staging.DEBUG: empty info
[2021-04-16 08:19:46] staging.DEBUG: staging
[2021-04-16 08:19:46] staging.DEBUG: 1
[2021-04-16 08:19:46] staging.DEBUG: end staging
[2021-04-16 08:19:46] staging.DEBUG: info foreach
[2021-04-16 08:19:46] staging.DEBUG: {"file_id":1729467,"peg_id":197805022009012001,"file_nama":"[6950] Sertifikat","file_lokasi":"197805022009012001\/197805022009012001_[6950] Sertifikat.pdf","file_ket":"Sertifikat","file_tgl":"2021-04-16T14:22:10.000000Z","created_at":null,"updated_at":"2021-04-16T07:22:10.472371Z","create_username":null,"update_username":null,"m_spg_file_pegawai_id":40,"entity_id":6950}
[2021-04-16 08:19:46] staging.DEBUG: end info foreach
[2021-04-16 08:19:46] staging.DEBUG: ----return info----
[2021-04-16 08:19:46] staging.DEBUG: {"file_id":1729467,"peg_id":197805022009012001,"file_nama":"[6950] Sertifikat","file_lokasi":"197805022009012001\/197805022009012001_[6950] Sertifikat.pdf","file_ket":"Sertifikat","file_tgl":"2021-04-16T14:22:10.000000Z","created_at":null,"updated_at":"2021-04-16T07:22:10.472371Z","create_username":null,"update_username":null,"m_spg_file_pegawai_id":40,"entity_id":6950}
[2021-04-16 08:19:46] staging.DEBUG: ----end return info----
[2021-04-16 08:19:46] staging.DEBUG: ----return info----
[2021-04-16 08:19:46] staging.DEBUG: array (
)
[2021-04-16 08:19:46] staging.DEBUG: ----end return info----
The reason you see two returns is simple - your function calls itself!
From the debug info, we can see the following:
The first time it runs, $info
must be empty, so it goes into the if
block (because we see empty info
in the log).
At that point, the function calls itself again. Therefore the next block of logs (from staging
until the first time we see ----end return info----
) is all from this second run of the function.
However, once that completes, it returns control to the first run - and because $info
is still empty in that version, you then see the second ----return info----
log, and the empty array.
So that explains what you're seeing. But I suspect you've missed something - there's very little point in calling your function again unless you're going to do something with the data it returns. At the moment your code ignores what the second call of the function is returning - even though that's the one which contains the data!
You should change this:
SyaratPengajuan::getFileSerdikTerakhir($pegawai, true);
to this:
$info = SyaratPengajuan::getFileSerdikTerakhir($pegawai, true);
This will populate $info
in the first run of the function with the data returned from the second run, and therefore provide that original caller with the correct data (as well as populating the logs with the data too).