-1

I only want to display the latest data from the tanggal_ins that I have marked, how to?

This is my json data: enter image description here

And this is my query:

$cuti = DB::table('t_cuti as cuti')
        ->join('m_karyawan as kary', 'kary.nik', '=', 'cuti.nik')
        ->join('m_rumah_sakit as rs', 'rs.kd_rs', '=', 'kary.kd_rs')
        ->join('m_unit as unit', 'unit.kd_unit', '=', 'kary.kd_unit')
        ->join('m_jabatan as jab', 'jab.kd_jabatan', '=', 'kary.kd_jabatan')
        ->leftJoin('t_cuti_adjust as adj', function ($leftJoin) {
            $leftJoin->on('adj.nik', '=', 'cuti.nik')
                ->latest();
        })
        ->select('adj.tanggal_ins','cuti.saldo_cuti','kary.nik','kary.nm_karyawan','rs.nm_rs','unit.nm_unit','jab.nm_jabatan')
        ->whereYear('adj.tanggal_ins', '<', $year)
        ->orWhere('adj.tanggal_ins', null)
        ->get();

Thanks.

6
  • 1
    Have you tried the limiting options described in the docs? I suggest reading the docs, they are the best place to start. Commented Dec 11, 2019 at 10:25
  • i already change tanggal_ins to created_at, and only enabled created_at like this stackoverflow.com/a/29886522/12128603. but keep doesn't worked
    – Fenz
    Commented Dec 11, 2019 at 11:18
  • I have tried using groupBy, but the value of created_at not display: pastebin.com/r33V7ELk you can see this result on this link pasteboard.co/IKQhujS.png i need more help from you, thanks..
    – Fenz
    Commented Dec 12, 2019 at 2:22
  • i think the problem in left join, cause i already tried limit in laravel ->take(1) inside left join but doesnt worked. and, when i tried outside leftjoin its worked for limit
    – Fenz
    Commented Dec 12, 2019 at 2:49
  • 1
    It is very hard to understand what you are trying to do. Your question title says show only last record, and the text says display the latest data. OK, that is pretty clear. To do that in a DB query you would first sort your results, and then limit the number you retrieve. For eg ORDER BY created_at, and LIMIT 2. In Laravel query builder that would be for eg orderBy() and limit() (I linked to the docs already). But your query has no ordering, and no limiting ... Commented Dec 12, 2019 at 9:18

1 Answer 1

1

Use orderBy.

->orderBy('id', 'desc')->first();

This will make descending order by ID. ->orderBy('id', 'desc')

and use first() instead of get().

1
  • 1
    the above problem has been resolved, I use the basic select from laravel and combined with the select function from postgreSQL, thanks for your response.
    – Fenz
    Commented Dec 13, 2019 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.