Created
July 15, 2021 19:18
-
-
Save therobfonz/ca71939c3b76fb24e125f1d80e48380a to your computer and use it in GitHub Desktop.
Eloquent Query Builder Macro to output raw sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
// output | |
select * from model where field = 'value'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment