To Raw SQL

https://repository-images.githubusercontent.com/597449262/33ee3d62-704b-4602-9d5e-f87aefc18f17

GitHub Workflow Status

To Raw SQL

Get raw SQL from Laravel Query Builder

Installation

composer require pyaesoneaung/to-raw-sql

Usage

User::where('votes', '>', 100)
    ->orWhere(function ($query) {
        $query->where('name', 'Abigail')
            ->where('votes', '>', 50);
    })
    ->toRawSql();

// "select * from `users` where `votes` > 100 or (`name` = 'Abigail' and `votes` > 50)"

Why?

toSql() doesn’t bind values to it. Instead, it returns the raw SQL representation of the query as a string, including placeholders for any bound values.

In some cases, you might want to inspect the actual SQL, with the values already bound to the placeholders.

Testing

License

The MIT License (MIT). Please see License File for more information.

Laravel News Links