/laravel-raw-sql

Get raw sql from laravel query builder via toRawSql method.

Primary LanguagePHPMIT LicenseMIT

Laravel Raw SQL

Get raw sql from laravel query builder via toRawSql method.

MIT licensed Build Status Coverage Status Scrutinizer Code Quality Code Intelligence Status Quality Gate Status Reliability Rating Security Rating Maintainability

Background

Using toSql method to output the sql of query builder, it's something with ?, not the raw sql.

use Illuminate\Support\Facades\DB;
use App\Models\User;


echo DB::table('user')->where('id', 1)->where('verified', 1)->toSql();
// or 
echo User::where('id', 1)->where('verified', 1)->toSql();

The output would be something like this with ?:

select * from `user` where `id` = ? and `verified` = ?

I exactly want the raw SQL like this:

select * from `user` where `id` = 1 and `verified` = 1

Now with this package, we can get the raw sql via toRawSql.

echo DB::table('user')->where('id', 1)->where('verified', 1)->toRawSql();
// or 
echo User::where('id', 1)->where('verified', 1)->toRawSql();

Will output

select * from `user` where `id` = 1 and `verified` = 1

Installation

Require this package with composer.

composer require xinningsu/laravel-raw-sql

As this package using laravel Package Discovery to discover Sulao\RawSql\ServiceProvider::class, so there's no need to do anything else, please use it directly.

License

MIT