Get raw sql from laravel query builder via toRawSql method.
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
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.