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

Asked

Viewed 644 times

2

How do I get the result of query which 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 the Eloquent is riding this query:

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

What method do I recover select pure that was assembled by Eloquent?

1 answer

2


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

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

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();
  • worked thanks!

  • You’re welcome @Marcelogomes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.