Home > PHP Framework > Laravel > Teach you how to easily output complete SQL statements in Laravel

Teach you how to easily output complete SQL statements in Laravel

藏色散人
Release: 2020-07-17 13:18:08
forward
4062 people have browsed it

下面由Laravel教程栏目给大家介绍Laravel中轻松容易的输出完整的SQL语句的方法,希望对需要的朋友有所帮助!


laravel 中自带的查询构建方法 toSql 得到的 sql 语句并未绑定条件参数,类似于这样 select * fromuserswhereid= ?,所以写了个扩展包 laravel-dump-sql ,可以获取完整的 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 文件中配置方法名称既可

<?php
return [
    /*
     * Get sql statement.
     */
    &#39;to_raw_sql&#39; => &#39;Your favorite method name&#39;,

    /*
     * Print SQL statements.
     */
    &#39;dump_sql&#39; => &#39;Your favorite method name&#39;,

    /*
     * Print SQL statements and exit.
     */
    &#39;dd_sql&#39; => &#39;Your favorite method name&#39;,
];
Copy after login

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!

Related labels:
source:learnku.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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template