Query Builders

Query builders in PyroCMS work just the same as query builders in Laravel with the addition of a few cool features.

Caching Queries

The query builder in PyroCMS provides a direct API for storing query results in the model's cache collection. The cache collection is automatically cleared when the model's table is altered in any way (such as saving, deleting, etc).

You can set the cache lifetime in seconds by using the ttl method:

$results = $model->where('status', true)->ttl(300)->get();

Caching queries uses the resulting query string including bindings to determine uniqueness.

Enabling Database Cache

You can enable the database cache via the control panel in Settings > System > Database. You can also publish the streams configuration and modify the database.php file or lastly define DB_CACHE=true in your .env file.

Fetching Fresh Results

If you are leveraging query caching you may want to temporarily omit cache. You can do this by using the fresh method.

$results = $model->where('status', true)->fresh()->get();

Manually Flushing Cache

Model related cache is flushed automatically when entries are updated, created, or deleted.

You can manually flush the cache by using the model's flushCache method:

$model->flushCache();

You can also clear all Laravel cache including database cache using php artisan cache:clear.