Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Venkatakrishnai's avatar

hi all,please convert the give raw query to laravel EOLQUENT

plase convert the given rawsql query to laravel eloquent

$students=DB::select('select s.id,count(se.id) as month,(select count(id) from student_enrollment_units_new where student_id=s.id and date between CURDATE() - INTERVAL 7 DAY AND CURDATE() and status="enrolled") as week from students as s left outer join student_enrollment_units_new as se on (s.id=se.student_id) and se.date between CURDATE() - INTERVAL 30 DAY AND CURDATE() and se.status="enrolled" and s.active=1 and s.deleted_at is null group by s.id');

0 likes
1 reply
mballaag's avatar

First of all can you put your code in the ``` so everyone can actually read this: https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code

but try this:

$students=DB::table('students as s')
      ->left('student_enrollment_units_new as se', 's.id', '=', 'se.student_id','left outer')
     ->select(' s.id',DB::raw('count(se.id) as month,(select count(id) from student_enrollment_units_new where student_id=s.id and date between CURDATE() - INTERVAL 7 DAY AND CURDATE() and status="enrolled") as week'))
      ->whereBetween('se.date', [Carbon::now()->subDays(30), Carbon::now()])
      ->where('se.status',"enrolled")
      ->where('s.active',1)
    ->whereNull('s.deleted_at')
    ->groupBy('s.id')->get();

Don't forget to use

use Carbon\Carbon;
1 like

Please or to participate in this conversation.