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).
To enable query cacheing you must first set DB_CACHE=true
in your .env
file and set the cache lifetime.
You can set the cache lifetime in seconds
by using the ttl
method:
$results = $model->where('status', true)->cache(300)->get();
You can also define the $ttl
property on the model you are using for the query:
protected $ttl = 300;
**Note:** Caching queries uses the resulting query string including bindings to determine uniqueness.
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();