# Loading Stats events from the database

Just like when [loading SystemLogEvents from the database](/guide/log-guide/loading-systemlog-events-from-the-database.md), you can use the provided [StatsEvents](/reference/core-classes/statsevents.md) class to retrieve and work with lists of [StatsEvent](/reference/core-classes/statsevent.md) objects.

For example, let's load the last fifty `StatsEventHomeView` objects from our [earlier example](/guide/stats-guide.md), using what we learned in the [Item lists](/guide/items-guide/item-lists.md) section of the [Items guide](/guide/items-guide.md):

```php
$statsEventItems = new \Cherrycake\Stats\StatsEvents([
    "p" => [
        "type" => "CherrycakeApp\StatsEventHomeView",
        "limit" => 50
    ]
]);

foreach ($statsEventItems as $statsEvent) {
    echo
        $e->Locale->formatTimestamp($statsEvent->timestamp).
        ": ".$statsEvent->typeDescription.
        ": ".$statsEvent->count.
				"\n";
}
```

As you can see, the [StatsEvents](/reference/core-classes/statsevents/statsevents-methods.md#fillfromparameters) class accepts some additional keys on top of the usual [Items::fillFromParameters](/reference/core-classes/items/items-methods.md#fillfromparameters) keys:

* **`type`** Retrieves only [StatsEvent](/reference/core-classes/statsevent.md) objects of this class name. Must include the full namespace route to the class.
* **`fromTimestamp`** Retrieves [StatsEvent](/reference/core-classes/statsevent.md) objects triggered after this timestamp.
* **`toTimestamp`** Retrieves [StatsEvent](/reference/core-classes/statsevent.md) objects triggered up to this timestamp.

```
5/21/20: Home view: 90
5/22/20: Home view: 1
```

{% hint style="success" %}
See examples of loading StatsEvent object from the database in the [Triggering a stats event ](https://documentation-examples.cherrycake.io/example/statsGuideTriggeringEvent)and [Events with additional dimensions](https://documentation-examples.cherrycake.io/example/statsGuideAdditionalDimensions) examples.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cherrycake.tin.cat/guide/stats-guide/loading-stats-events-from-the-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
