mardi 18 octobre 2016

How to write raw query in Laravel 5.1?

I am beginner in Laravel.I can write query in raw PHP.Now i want to write it in Laravel framework.I have read documentation and tried so far:

In Raw PHP:

SELECT count(*)/(TIMESTAMPDIFF(YEAR,'2016-01-01 00:00:00',now())+1) as avg from buyer  where created_at >= '2016-01-01 00:00:00';

Tried with Laravel:

public function index()
{
    //
    $yrUserReg = DB::table('buyer')
                    -> SELECT (DB::raw (count('*')/("TIMESTAMPDIFF(YEAR,'2016-01-01 00:00:00',now())+1"))) 
                    -> AS ('avg') 
                    -> WHERE ("created_at >= '2016-01-01 00:00:00'");
   return view('userflow',compact($yrUserReg));                 
}

But it throws me an error: Division by Zero.I can not find any solution of this. Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire