-1

I have a query in normal SQL below and I want to change it into artisan tinker query,

select count(P.id), DATEDIFF(P.tarehe_ya_kujifungua,P.tarehe_ya_kuzaliwa) as 
umri from postnatals P INNER JOIN register13s R ON P.namba_ya_kadi_RCH4 = 
R.Namba_ya_kadi WHERE EXTRACT(MONTH FROM tarehe_ya_hudhurio) = EXTRACT(MONTH 
FROM '2018-06-12') group by  P.id HAVING count(DISTINCT R.hudhurio) > 1 AND 
umri >= 7305;

I have a model Postnatal.php for postnatals table and also I have Register13.php for register13s table.

Parth Pandya
  • 1,050
  • 7
  • 15
  • 22
Angelwise
  • 101
  • 1
  • 5

1 Answers1

1

Thats how you can convert this query to Laravel Query builder.

DB::table('postnatals as P')
    ->innerJoin('register13s as R', 'P.namba_ya_kadi_RCH4','=', 'R.Namba_ya_kadi')
->whereRaw('EXTRACT(MONTH FROM tarehe_ya_hudhurio) = EXTRACT(MONTH FROM '2018-06-12')')
->groupBy('P.id')
->having(DB::raw('count(DISTINCT R.hudhurio)'),'>',1)
->having('umri', '>', 7305)
->selectRaw('select count(P.id), DATEDIFF(P.tarehe_ya_kujifungua,P.tarehe_ya_kuzaliwa) as umri')
->get()

Hope this helps.

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66