Eloquent toRawSQL method
In AppServiceProvider boot method:
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());
});
Result:$books = Book::where('author', 'Ruskin Bond')->toRawSql();
dd($books);
// select * from `books` where `author` = 'Ruskin Bond'
Comments
Post a Comment