1

I'm having problem to view data using db::select.

Here is my full sql statement [Tested and return value as expected on mysql]

SELECT tugasan_kontrak.*,sejarah.tkh_wujud AS tkh_wujud ,sejarah.id_tindakan AS id_tindakan 
FROM tugasan_kontrak 
LEFT JOIN sejarah ON(
tugasan_kontrak.id_tugasan = sejarah.id_tugasan AND 
jenis_tugasan='k' AND 
sejarah.id_tindakan !='99' AND
id_sejarah = (SELECT MAX(id_sejarah) FROM sejarah WHERE 
    tugasan_kontrak.id_tugasan = sejarah.id_tugasan AND jenis_tugasan='k') 
)
WHERE pegawai_tugas='890911105098' AND id_tindakan IS NOT NULL

Result

But when I try to apply this statement using db::select, no record were view.

Here the function I use to test

public function getPenggunaTindakan($value='890911105098')
{
    $kontrak = DB::select("select tugasan_kontrak.*,sejarah.tkh_wujud as tkh_wujud ,sejarah.id_tindakan as id_tindakan 
            from tugasan_kontrak left join sejarah on(tugasan_kontrak.id_tugasan = sejarah.id_tugasan and jenis_tugasan='k' and sejarah.id_tindakan !='99' and
                id_sejarah = (select max(id_sejarah) from sejarah where tugasan_kontrak.id_tugasan = sejarah.id_tugasan and jenis_tugasan='k'))
        where pegawai_tugas=:nokp and id_tindakan is not null ",['nokp' => $value]);

    dd($kontrak);
}

What did I do wrong?

1

1 Answer 1

1

Try the following :

DB::select(DB::raw("select tugasan_kontrak.*,sejarah.tkh_wujud as tkh_wujud ,sejarah.id_tindakan as id_tindakan 
            from tugasan_kontrak left join sejarah on(tugasan_kontrak.id_tugasan = sejarah.id_tugasan and jenis_tugasan='k' and sejarah.id_tindakan !='99' and
                id_sejarah = (select max(id_sejarah) from sejarah where tugasan_kontrak.id_tugasan = sejarah.id_tugasan and jenis_tugasan='k'))
        where pegawai_tugas=:nokp and id_tindakan is not null "),['nokp' => $value]);

If you want to inject sql query, you have to use DB::raw in laravel

Sign up to request clarification or add additional context in comments.

2 Comments

thank its working. but need to fix the ) should put before is not null") ...thanks a lot ...u save my day ^_^
@Muthu17 Maybe you can help me. Look at this : stackoverflow.com/questions/51838922/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.