Home> PHP Framework> Laravel> body text

Easily output complete SQL statements in Laravel

藏色散人
Release: 2021-03-24 09:03:09
forward
2718 people have browsed it
laravel 中自带的查询构建方法 toSql得到的 sql 语句并未绑定条件参数,类似于这样 select * fromusers whereid = ?,所以写了个扩展包 laravel-dump-sql ,可以获取完整的 sql 语句。

源码

  • laravel-dump-sql - https://github.com/guanguans/laravel-dump-sql

安装

$ composer require guanguans/laravel-dump-sql -v
Copy after login

发布服务

$ php artisan vendor:publish --provider="Guanguans\\LaravelDumpSql\\ServiceProvider"
Copy after login

使用

安装成功后查询构建器会新增 toRawSqldumpSqlddSql三个方法
// 获取 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();
Copy after login

自定义方法名称

发布配置文件

$ php artisan vendor:publish --tag=laravel-dump-sql
Copy after login

config/dumpsql.php文件中配置方法名称既可

 'toRawSql', /* * Print SQL statements. */ 'dump_sql' => 'dumpSql', /* * Print SQL statements and exit. */ 'dd_sql' => 'ddSql', ];
Copy after login

推荐:最新的五个Laravel视频教程

The above is the detailed content of Easily output complete SQL statements in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!