Tất cả các phương thức Model thao tác với Database P2 (ok)
https://tinkerwell.app/blog/how-to-get-the-raw-sql-query-from-the-laravel-query-builder
Last updated
https://tinkerwell.app/blog/how-to-get-the-raw-sql-query-from-the-laravel-query-builder
Last updated
Sometimes, you ask yourself how you can get the Laravel query builder to output its raw SQL query as a string. Luckily, there are multiple ways how to get this raw query.
The first method to get the query of an Eloquent call is by using the toSql()
method. This method returns the query without running it – good if you don't want to alter data and only get the query – but this method doesn't show the whole query if your query is more complex or if there are sub-queries.
Output:
The second method is the Laravel query log that collects all queries within a request. You can enable this log, run your query and dump the output.
This code leads to the output:
This gives you detailed information about the executed query and their execution time. If the query has data bindings, the query logs lists them and makes it easy to check your data,
While you can get the raw SQL query with the methods above, there are situations where you debug the runtime or memory allocation of a query and want to know why it takes longer than expected.
If you write your query in Tinkerwell anyway, you can highlight the Eloquent query and press Cmd+Shift+R
(or Ctrl+Shift+R
if you are on Windows) and Tinkerwell profiles the query directly in the editor. You see all executed queries, the time that the query run, the memory consumption and even the peak for your memory. This is super useful if you debug server crashes or want to know how much memory your application server needs.
There are multiple ways how to get the raw SQL query for an Eloquent call – we are using Tinkerwell daily and so this is our natural way to debug queries.
The must-have companion to your favorite IDE. Quickly iterate on PHP code within the context of your web application.
The code runner for PHP. Loved by developers like you.
DocumentationChangelogDownload
Affiliate ProgramOpen SourceEducation
Learn moreTinkerwell