-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Adds toRawSql() method on both Query and Eloquent builder #39551
Conversation
Is this also going to work with the new compound Builder intererfaces in Laravel 9? |
There was an attempt not long ago: #38027 As a postgres user myself, this is the first coming to my mind too from #38027 (comment) (not that there's a problem, but the question about if it's compatible):
|
See comments from @mfn |
There is also the possibility that Post::whereRaw('likes > :likecount and type = :type', ...) May be the exception but certainly used as seen here: |
But this has a huge security risk, this allows for sql injections Let's say we have a Writing this User::query()->where('username', "melek\' OR 1 = 1 #")->toRawSql() Will generate this sql select * from `users` where `username` = 'melek\\' OR 1 = 1 #' |
I don't think it's intended to run the returned string as a query, so it's not that important that the query isn't safe.
When having a complex query with multiple joins, groups and orders the |
I think the security concerns could be mitigated by introducing this as In our app we have a |
The code could use |
As @dennisprudlo pointed out, this isn't intended to be used to run the returned string as a query but instead as a development tool. If that is a concern, it can be mitigated (i.e. using @tpetry's suggestion or changing the method to something like Now the |
It's 2021, there is no reason to even build an sql string with sql injections for "development purposes". When not escaping the values because it's just dumping them, the developer still has to manually fix all the sql injections before being able to run the query. And it's not any problem making it safe for multiple years now. You can simply let pdo the quoting as is i did here and you'll get a safe query string. (Bonus point: It's also correct for the special PostgreSQL operators containing a question mark) Named sql placeholders and obscure raw sql query parts are difficult to solve, and you'll get in a deep rabbit hole when trying to fix them. I simply omitted them. Because the following query could only be correctly transformed to a raw sql query by using a full sql parser (which would need to be specific for every rdbms too): DB::table('test')->whereRaw("col1 = 'why are ''you'' doing this?' AND col2 = ?", ['value'])-> toRawSql(); |
Please mark this as ready for review if you want this to be reviewed again. Otherwise we'll be closing this PR soon and it'll be better if you resend this in when it's finished. |
What does it do?
This PR adds
toRawSql()
both the Query and Eloquent builders which will output the raw SQL query with the bindings inline.Does it break anything?
No, these a new methods that do not conflict with any existing methods.
Benefits
The benefit is logging complete queries instead of an array of the query and the bindings separately. This helper method returns a string with the bindings inserted into the query. Seems this is popular request on Twitter.
Tests
Tests are added for both Query and Eloquent builders