-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Add possibility to output an sql inlining the bindings to the screen #39053
Conversation
@dietercoopman in my opinion returning a string is better here is different ideas then echoing the sql string and still returning the Query Builder.: $result = $query->show(function(string $sqlWithBindings) {
Log::info($sqlWithBindings);
})->get(); If you return a string on the Builder::macro('xxx', function($query) {
$sqlWithBindings = $query->show();
// Do something with the string
return $query;
}); I think the second approach would be best. Meaning that show would return a string. |
Hi @Jubeki , thx for your feedback 👌 , I've added the possibility to add a callback , so you can decide not to output the sql statement to the screen but logging it to whatever you want. I'm a big fan of not breaking the chain on the builder, but I Am open for any suggestion and of course will listen to the community wishes 👏 $callback = function(string $sql){
Log::info($sql);
};
DB::table('products')->where('id', '=', 1)->show($callback)->get(); |
Similar PR has been previously declined in #38027. Please consider some of the implementation there, especially for the binding substitution logic. |
Hi @chu121su12 , thx for your feedback 👏 I've updated the code, so it uses a better substitution logic. Hope we can have a framework included candidate for this functionality. I've already created a package for this but I think this might be a timesaver to have it included. |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
I've created a package past week that implements this features and I've enhanced it with the feedback given , thx guys ! 👏 If you are interested ⏬ |
What ?
A trait added to the Eloquent Builder and Query Builder to make it possible to echo the current sql with bindings included on the builder
Usage
The Query Builder
The Eloquent Builder
@Jubeki gave some valuable feedback 👏 and I've added the possibilty to pass a callback if you don't want the echo output. So you can decide whatever you want to do with the sql statement.
Why
We often want to show the query ( including the filled in bindings ) that will run against our database server. You can do that with many of the community driven profiling tools but they are mostly thrown in a container together with other sql statements. So you have to start dig into the bunch of statements ran against your database. I find myself writing a macro over and over again to have the possibility of this pull request , maybe it might be a nice addition to the framework itself.
Thoughts
I now did the output to the screen , that's a choice , but also a doubt. I find it must return the Builder instance and not a string ( because not wanting to break the chain ). The doubt in it is that the user gets no choice ...It's always an output , and not a log entry. EDIT after the feedback from @Jubeki i've made it possible to pass it a closure
Could extend it but might be too much complex for developers using it. What's your opinion on that ?