Is possible to dump a query made on a twig template for a statement like this one for example:


Can we some how see the actual SQL query ?

2 Answers 2


You can see each SQL query that is run for a page by turning devMode on and opening your console to read the 'Profiling Summary Report'.

You can see a total query count and each query beings with system.db.CDbCommand.query.

I'd advise maybe isolating the twig code on its own template to avoid having to work out which query was a result of the twig code you're after.

  • You can also try to filter the rows returned to the console to find the right one. In this example using "homepage" as a search term.
    – carlcs
    Commented Jan 12, 2017 at 15:11
  • Yeah this actually what i did, i isolated the twig code but instead of looking at the 'Profiling Summary Report' i used a plugin named Query log.The 'Profiling Summary Report' is much easier cause no extra plugin is needed.
    – nickGR
    Commented Jan 13, 2017 at 10:42

In Craft 3 you can use getRawSql() at the end of the element query.

{% set entries = craft.entries.section('blog').getRawSql() %}

{{ entries }}
  • Brilliant - thanks so much! Very useful for debugging twig api templates. Commented Mar 10, 2022 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.