Home >PHP Framework >Laravel >Teach you how to easily output complete SQL statements in Laravel
下面由Laravel教程栏目给大家介绍Laravel中轻松容易的输出完整的SQL语句的方法,希望对需要的朋友有所帮助!
laravel 中自带的查询构建方法
toSql
得到的 sql 语句并未绑定条件参数,类似于这样select * from
userswhere
id= ?
,所以写了个扩展包 laravel-dump-sql ,可以获取完整的 sql 语句。
$ composer require guanguans/laravel-dump-sql -v
$ php artisan vendor:publish --provider="Guanguans\\LaravelDumpSql\\ServiceProvider"
安装成功后查询构建器会新增
toRawSql
、dumpSql
、ddSql
三个方法
// 获取 SQL User::where('id', 1)->toRawSql(); DB::table('user')->where('id', 1)->toRawSql(); // 打印 SQL User::where('id', 1)->dumpSql(); DB::table('user')->where('id', 1)->dumpSql(); // 打印 SQL 并退出 User::where('id', 1)->ddSql(); DB::table('user')->where('id', 1)->ddSql();
$ php artisan vendor:publish --tag=laravel-dump-sql
config/dumpsql.php
文件中配置方法名称既可<?php return [ /* * Get sql statement. */ 'to_raw_sql' => 'Your favorite method name', /* * Print SQL statements. */ 'dump_sql' => 'Your favorite method name', /* * Print SQL statements and exit. */ 'dd_sql' => 'Your favorite method name', ];
The above is the detailed content of Teach you how to easily output complete SQL statements in Laravel. For more information, please follow other related articles on the PHP Chinese website!