Created
May 29, 2023 08:01
-
-
Save vmrfriz/4e48a414ea2299c52e11a857fa390a8d to your computer and use it in GitHub Desktop.
Laravel toSql with parameters
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
<?php | |
// File app/Providers/AppServiceProvider.php | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
function boot(): void | |
{ | |
Schema::defaultStringLength(191); | |
\Illuminate\Database\Query\Builder::macro('dumpSql', function () { | |
$bindings = $this->getBindings(); | |
$sql = str_replace('?', '%s', $this->toSql()); | |
$sql = sprintf($sql, ...$bindings); | |
dump($sql); | |
}); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register(): void | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Когда $builder->toSql() недостаточно, на помощь приходит метод $builder->dumpSql(), который для начала нужно объявить в AppServiceProvider (или другом сервис провайдере).