How to get the result of the query executed in Eloquent?

2

How do I get the result of query that will be executed in the database?

Example:

foreach ($prestadores as $prestador){
  $achou = \DB::table('modulo_pr_espelho')
           ->where('prestador_id','=', $prestador->id)
           ->whereMonth('data_emissao',$mes)->get();
     if (!isset($achou)) {
           print "Prestador : " . $prestador->razao_social;
     }
}

I need to know how Eloquent is mounting this query :

select modulo_pr_espelho.* where prestador_id = 1 where month(data_emissao) = 11

What method do I retrieve the select pure that was set by Eloquent ?

    
asked by anonymous 14.11.2016 / 21:44

1 answer

2

You can use the toSql () method on the builder:

$achou = \DB::table('modulo_pr_espelho')
            ->where('prestador_id','=', $prestador->id)
            ->whereMonth('data_emissao',$mes)->toSql();

In this way you do not have access to the parameters of the query (bindings), to get the array with the parameters can:

$achou = \DB::table('modulo_pr_espelho')
            ->where('prestador_id','=', $prestador->id)
            ->whereMonth('data_emissao',$mes)->getBindings();
    
14.11.2016 / 22:00