7

I have installed latest Drupal 8.1.1 also Devel module installed there is no any option for Display query log on devel setting page while it's present on D7 Devel module. I have created a view with relationship now I want to display SQL Query of that view at the front end.

How do I print the SQL query of a view in front end?

4

The Devel module includes the webprofiler module; you have to enable that to see the query log.

Note that there's currently a bug in core in 8.1 that prevents it from being used.

6

You can enable on "Show the SQL query" and "Show other queries run during render during live preview" on "www.example.com/admin/structure/views/settings"

enter image description here

Hope it will help you.

  • Thnx for the fast reply, all the options are already enabled("Show the SQL query" and "Show other queries run during render during live preview") this is only for admin preview I want this one for end user(authenticated and anonymous user). – Sanjay May 21 '16 at 6:17
4

While you could install webprofiler module, if you've already got a custom module, it might be quicker to do this:

/**
 * Implements hook_views_post_execute().
 */
function MY_MODULE_views_post_execute(ViewExecutable $view) {
  if ($view->id() != 'MY_SPECIAL_VIEW') {
    return;
  }
  dpq($view->query->query());
}
2

If you're using the database abstraction layer to run your own queries, you can do use the addTag method and pass in 'debug', too.

$query->addTag('debug')

1

You can simply do dpq($query->query()); That will put the query in the message area of your frontend. Edit - You will need devel module to use this feature.

0
$query = $db->query('MY QUERY');
print_r($query); // In the result you'll see a key "queryString" containing the actual query
0

If someone is looking for the Drupal 7 equivalent of Dalin's answer, the structure is just a little different.

/**
 * Implements hook_views_post_execute().
 */
function MY_MODULE_views_post_execute($view) {
  if ($view->name != 'MY_SPECIAL_VIEW') {
    return;
  }
  dpq($view->build_info['query']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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