I'm converting my web application to Laravel framework but I have a specific problem trying to write a particular raw SQL query in Laravel eloquent.
Here's the query:
What I've tried so far:
$cp = DB::table('career_progression')
->select('staff_id', DB::raw('MAX(cpEffectiveDate) as max_date'))
->groupBy('staff_id');
$staffs = DB::table('staff')
->leftJoinSub($cp, 'cp', function ($join) {
$join->on('staff.id', '=', 'cp.staff_id');
})
->select(DB::raw('staff.firstname, staff.middlename, staff.lastname, staff.state, staff.lga, cp.cpEffectiveDate, cp.designation, cp.grade,
cp.step'));
This is what I want to achieve:
SELECT s.firstname, s.middlename, s.lastname, s.state, s.lga, cp.cpEffectiveDate,cp.designation, cp.grade, cp.step FROM staff s LEFT JOIN career_progression cp ON cp.staff_id = s.id JOIN (SELECT a.staff_id, MAX(a.cpEffectiveDate) AS max_date FROM career_progression a GROUP BY a.staff_id) x ON x.staff_id = cp.staff_id AND x.max_date = cp.cpEffectiveDate
Any hint/help will be much appreciated, thanks.