Count total of children


I am counting children and filter them on company name (in my example is de name of the company: DesignStudio).

<?php if($articles = $page->children()->filterBy('company', 'DesignStudio', ',')): ?> (<?= $articles->count() ?>)
<?php endif ?>

This works fine.

Now I want to count the total of companies. And tried this:

<?php if($articles = $page->children()->filterBy('company')): ?> (<?= $articles->count() ?>)
<?php endif ?>

But this doesn’t work. Can anyone help me?

First of all, that if-statement doesn’t make sense at all. $articles will always be a pages collection, no matter if it contains elements or not.

$articles = $page->children()->filterBy('company', 'DesignStudio', ',');
if ($articles->isNotEmtpy()) {
  echo $articles->count();

The if statement is not necessary, only if you only want to print the number of articles if there are articles.

And what is the total number of companies? All entries in this field? Then you don’t want to filter anything but pluck() the companies from the pages.

$companies = $articles->pluck('company', ',', true);
$companyCount = count($companies);

But maybe you want something else here?