Skip to content

Instantly share code, notes, and snippets.

View therobfonz's full-sized avatar

Rob Fonseca therobfonz

View GitHub Profile
@therobfonz
therobfonz / raw_query.php
Created July 15, 2021 19:18
Eloquent Query Builder Macro to output raw sql
use Illuminate\Database\Eloquent\Builder;
Builder::macro('toRawSql', function(){
return array_reduce($this->getBindings(), function($sql, $binding){
return preg_replace('/\?/', is_numeric($binding) ? $binding : "'".$binding."'" , $sql, 1);
}, $this->toSql());
});
// use
Model::where('field', 'value)->toRawSql();

Keybase proof

I hereby claim:

  • I am therobfonz on github.
  • I am therobfonz (https://keybase.io/therobfonz) on keybase.
  • I have a public key ASBJqvaB1M2rQsZpzsYWEJ7zxROX1s1LfWaub_fNNzlwKwo

To claim this, I am signing this object: