---
title: Repositories
---

# Repositories

<div class="documentation__toc"></div>

## Introduction

Because the Streams Platform treats models like entities it's design dictates that we put access/storage logic into `repositories`. These repositories simply wrap the models themselves to pass along controlled access/storage methods.

This document will point out plugin functionality where it matches repositories. Even though they are two different classes responsible.
 
### Collections

Where noted results are returned as `\Anomaly\Streams\Platform\Model\EloquentCollection` collections. Learn more about [collections here](../entries/collections).

## Base Repository

The base repository wraps regular `\Anomaly\Streams\Platform\Model\EloquentModel` instances. All repositories extending the base repository have these features.

### all

The `all` function simply returns all the non-trashed entries as an `\Anomaly\Streams\Platform\Model\EloquentCollection`:

```php
$collection = $repository->all();
```

```twig
{% set collection = entries(namespace, stream).all() %}
```

### find

The `find` function returns a single entry by it's `$id`:

```php
$entry = $repository->find($id);
```

```twig
{% set entry = entries(namespace, stream).find(id) %}
```

### findBy

The `findBy` function returns a single entry by it's unique `$attribute` and `$value`:

```php
$entry = $repository->findBy($attribute, $value);
```

```twig
{% set entry = entries(namespace, stream).findBy(attribute, value) %}
```

### findAll

The `findAll` function returns a collection of entries where in an array of `$ids`:

```php
$collection = $repository->findAll($ids);
```

```twig
{% set collection = entries(namespace, stream).findAll(ids) %}
```

### findAllBy

The `findAllBy` function returns a collection of entries where `$attribute` is `$value`:

```php
$collection = $repository->findAllBy($attribute, $value);
```

```twig
{% set collection = entries(namespace, stream).findAllBy(attribute, value) %}
```

### create

The `create` function creates a new entry with `$attributes`. Returns the created entry or `false` if creation fails.

```php
$result = $repository->create($attributes);
```

Remember when creating [translatable](../entries/translatable) entries you define translatable attributes as a `locale` attribute of values.

```php
$result = $repository->create([
    'name' => 'Ryan',
    'en' => [
        'title' => 'Developer'
    ]
]);
```

### newQuery

The `newQuery` function returns a new [query builder](../database/query-builder) instance.

```php
$query = $repository->newQuery();
```

Within views, a [criteria](../entries/criteria) instance is returned. Criterias are wrap query builders and restrict their functionality for security. Destructive actions are not possible within views.

```twig
{% criteria = query(table) %}
{% criteria = entry(model) %}
{% criteria = entry(namespace, stream) %}
```

### save

The `save` function saves changes applied to an `entry`. Returns `true` if successful or `false`.

```php
$result = $repository->save($entry);
```

### update

The `update` function updates all entries with the provided `$attributes`. Returns `true` if successful or `false`.

```php
$result = $repository->update($attributes);
```

### delete

The `delete` function deletes a given `entry`. Returns `true` if successful or `false`.

[Trashable](../entries/trashable) entries will be trashed until `forceDelete` is used or the trash is emptied. 

```php
$result = $repository->delete($entry);
```

### forceDelete

The `forceDelete` function deletes a given [trashable](../entries/trashable) `entry` without trashing it first. Returns `true` if successful or `false`.

```php
$result = $repository->forceDelete($entry);
```

### restore

The `restore` function restores a given [trashable](../entries/trashable) `entry` from it's trashed state. Returns `true` if successful or `false`.

```php
$result = $repository->restore($entry);
```

### truncate

The `truncate` function deletes all entries from the database. This can be helpful during for mass/import/refresh commands.

```php
$repository->truncate();
```

### withoutEvents

The `withoutEvents` function allows you to execute a `$closure` of repository logic to run without firing [model events](../entries/observers) like `saved` or `created`.

```php
$result = $repository->withoutEvents($closure);
```

```php
$users = app(Anomaly\UsersModule\User\Contract\UserRepositoryInterface::class);

if ($user = auth()->user()) {
    $users->withoutSyncingToSearch(
        function () use ($user) {
            $this->touchLastActivity($user);
        }
    );
}
```

### cache

The `cache` function allows you to cache the result of a `$closure` of logic for `$ttl` and bind the cached result to the model's `cache collection`.

When `flushCache` is fired on the model it's `cache collection` will be emptied and all the model cache cleared.

```php
$result = $repository->cache($key, $seconds, $closure);
```

```php
$roles = app(Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface::class);

$guest = $roles->cache(
    'anomaly.module.users::roles.guest',
    60 * 60 * 24, // 1 day
    function () use ($roles) {
        return $roles->findBySlug('guest');
    }
);
```

### cacheForever

The `cacheForever` function allows you to cache the result of a `$closure` of logic and `bind` the cached result to the model's `cache collection`.

The result will be cached forever or until `flushCache` is fired on the model.

```php
$result = $repository->cache($key, $seconds, $closure);
```

```php
$roles = app(Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface::class);

$guest = $roles->cacheForever(
    'anomaly.module.users::roles.guest',
    function () use ($roles) {
        return $roles->findBySlug('guest');
    }
);
```

### flushCache

The `flushCache` function clears all model cache including it's `cache collection`.

This function is automatically fired anytime an entry is `created`, `updated`, `deleted`, and any time it's database information is altered.

```php
$repository->flushCache();
```


## Entry Repository

Entry models are used for stream entry models specifically. They will have all the above functions as well.

### sorted

The `sorted` function returns all entries sorted by `direction` based on it's title column or [sortable](../entries/sortable) order if a sortable stream.

```php
$collection = $repository->sorted($direction = 'asc');
```

```twig
{% set collection = entries(namespace, stream).sorted(direction = 'asc') %}
```

### first

The `first` function returns the first entry sorted by `direction` based on it's title column or [sortable](../entries/sortable) order if a sortable stream.

```php
$entry = $repository->first($direction = 'asc');
```

```twig
{% set entry = entries(namespace, stream).first(direction = 'asc') %}
```

### lastModified

The `lastModified` function returns the last modified `entry` based on it's `created_at` and `updated_at` columns.

```php
$collection = $repository->sorted($direction = 'asc');
```

```twig
{% set collection = entries(namespace, stream).sorted(direction = 'asc') %}
```
