Repositories

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.

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:

$collection = $repository->all();
{% set collection = entries(namespace, stream).all() %}

find

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

$entry = $repository->find($id);
{% set entry = entries(namespace, stream).find(id) %}

findBy

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

$entry = $repository->findBy($attribute, $value);
{% set entry = entries(namespace, stream).findBy(attribute, value) %}

findAll

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

$collection = $repository->findAll($ids);
{% set collection = entries(namespace, stream).findAll(ids) %}

findAllBy

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

$collection = $repository->findAllBy($attribute, $value);
{% 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.

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

Remember when creating translatable entries you define translatable attributes as a locale attribute of values.

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

newQuery

The newQuery function returns a new query builder instance.

$query = $repository->newQuery();

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

{% 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.

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

update

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

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

delete

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

Trashable entries will be trashed until forceDelete is used or the trash is emptied.

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

forceDelete

The forceDelete function deletes a given trashable entry without trashing it first. Returns true if successful or false.

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

restore

The restore function restores a given trashable entry from it's trashed state. Returns true if successful or false.

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

truncate

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

$repository->truncate();

withoutEvents

The withoutEvents function allows you to execute a $closure of repository logic to run without firing model events like saved or created.

$result = $repository->withoutEvents($closure);
$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.

$result = $repository->cache($key, $seconds, $closure);
$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.

$result = $repository->cache($key, $seconds, $closure);
$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.

$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 order if a sortable stream.

$collection = $repository->sorted($direction = 'asc');
{% 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 order if a sortable stream.

$entry = $repository->first($direction = 'asc');
{% 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.

$collection = $repository->sorted($direction = 'asc');
{% set collection = entries(namespace, stream).sorted(direction = 'asc') %}