Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

How to count query results of entityQuery in drupal 8 & 9

 

To obtain a count query from an existing query, use the countQuery() method.

$count_query = $query->countQuery();

 

$count_query is now a new Dynamic Select query with no ordering restrictions that when executed will return a result set with only one value, the number of records that would be matched by the original query. Because PHP supports chaining methods on returned objects, the following idiom is a common approach:

$num_rows = $query->countQuery()->execute()->fetchField();

 

For an entity query (implementing QueryInterface), the code is slightly different:

$num_rows = $query->count()->execute();

 

Example How to count query results of entityQuery in drupal 8 & 9

 $query = \Drupal::entityQuery('node')

                  ->condition('type', 'page')

                   ->condition('user_id', $user_id);

$count = $query->count()->execute();

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon