Skip to content

Instantly share code, notes, and snippets.

@tomcastleman
Created September 13, 2017 12:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomcastleman/153020d0b1b4a77ae09f14bd70b4ceb1 to your computer and use it in GitHub Desktop.
Save tomcastleman/153020d0b1b4a77ae09f14bd70b4ceb1 to your computer and use it in GitHub Desktop.
Laravel Query Builder Dump SQL Macro
<?php
namespace App\Providers;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\ServiceProvider;
class DumpSqlServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
Builder::macro('dumpSql', function ($die = false) {
/** Builder $this */
dump($this->toSql());
$method = $die ? 'dd' : 'dump';
$method($this->getBindings());
});
}
}
@tomcastleman
Copy link
Author

tomcastleman commented Sep 13, 2017

Usage:

Post::where('id', '123')->dumpSql($die = true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment