---
title: Repositories
---

# Repositories

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

## Introduction

The `repository` pattern is how we separate `database` and `entity` code.

Repositories wrap a `model` and should provide all database operations (save, update, find, etc). Note that Laravel models can handle this and _they do_ handle it **within repositories**. The separation of logic and application purpose is what is gained. This is a crucial separation and is considered best practice in our architecture.

## Defining Repositories

Entry repositories are created when using the `make:addon` command.

Should you need to create one manually it would start like this:

```php
<?php namespace {namespace};

use Anomaly\Streams\Platform\Entry\EntryRepository;

class {class} extends EntryRepository
{

    /**
     * The entry model.
     *
     * @var {model}
     */
    protected $model;

    /**
     * @param {model} $model
     */
    public function __construct({model} $model)
    {
        $this->model = $model;
    }
}
```


## Fetching Results

Before you cna use a repository you must instantiate one through Laravel's [service container](../the-basics/service-container).

The following documentation will assume you have a scenario like this:

```php
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface;

$pages = app(PageRepositoryInterface::class);

return $pages->find($id);
```

### all

The `all` method returns a collection of all entries.

```php
$pages->all();
```

### allWithTrashed

The `allWithTrashed` method returns a collection of all entries including trashed ones.

```php
$pages->allWithTrashed();
```

### allWithoutRelations

The `allWithoutRelations` method returns a collection of all entries without relations.

```php
$pages->allWithoutRelations();
```

### find

The `find` method returns a single entry by `id`.

```php
$pages->find($id);
```

### findBy

The `findBy` method returns a single entry where `column` is `value`. 

```php
$pages->findBy($column, $value);
```

> For `entry` models the `column` could also be a `field_slug`{.tip}

### findAll

The `findAll` method returns a collection of entries in `$ids`. 

```php
$pages->findAll(array $ids);
```

### findAll

The `findAllBy` method returns a collection of entries where `column` is `value`. 

```php
$pages->findAllBy($column, $value);
```

> For `entry` models the `column` could also be a `field_slug`{.tip}

### findTrashed

The `findTrashed` method returns a collection of all trashed entries. 

```php
$pages->findTrashed();
```

### create

The `create` method creates a new entry with `attributes`. 

```php
$pages->create(array $attributes);
```

### newQuery

The `newQuery` method returns a new query builder instance for the contained model. 

```php
$pages->newQuery()->where('path', 'about/company')->first();
```

### newInstance

The `newInstance` method returns a new model/entry instance optionally with `attributes`. 

```php
$newPage = $pages->newInstance(array $attributes = []);
```

### count

The `count` method returns the total number of entries in the database. 

```php
$pages->count();
```

### paginate

The `paginate` method returns a paginator instance from `parameters`. 

```php
$pages->paginate(array $parameters = []);
```

You can create an optional `simple` paginator with parameters like this:

```php
$pages->paginate([
    'paginator' => 'simple',
    'per_page' => 5
]);
```

### save

The `save` method saves the `entry`. While this is possible with `save` on the entry model itself, if you are respecting the repository / domain entity pattern then this is a nice way to scratch that OCD itch. 

```php
$pages->save($page);
```

### withoutEvents

Use the `withoutEvents` method to perform an operation without model events like `saved` and `created`.
 
The repository instance is available as `$this` within the closure.

```php
$pages->withoutEvents(function() use ($page) {
    
    $page->slug = 'example';
    
    $this->save($page);
});
```

### update

Use the `update` method to update multiple entries with `attributes`.
 
The repository instance is available as `$this` within the closure.

```php
$pages->update(['enabled' => true]);
```

### delete

Use the `update` method to delete an `entry`.

```php
$pages->delete($page);
```

### forceDelete

Use the `forceDelete` method to delete an `entry` and **bypass trashing** if trashable.

```php
$pages->forceDelete($page);
```

### restore

Use the `restore` method to restore a trashed `entry`.

```php
$pages->restore($page);
```

### truncate

Use the `truncate` method to truncate the entry's database table.

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

### cache

Use the `cache` method to cache a `value` and bind it to the model until it's cleared or `ttl` is met.

When a model's data is modified in any way the cache bound to said model will be deleted as well.

```php
$pages->cache($key, $ttl, $value);
```

This can be used much like using Laravel's cache directly.

```php
$pages->cache('anomaly.modules.pages::first_page', 3600, function() use ($pages) {
    return $pages->first();
});
```

### cacheForever

Use the `cacheForever` method to cache a `value` and bind it to the model until it's cleared.

When a model's data is modified in any way the cache bound to said model will be deleted as well.

```php
$pages->cacheForever($key, $value);
```

This can be used much like using Laravel's cache directly.

```php
$pages->cacheForever('anomaly.modules.pages::first_page', function() use ($pages) {
    return $pages->first();
});
```

### flushCache

Use the `flushCache` method to manually bust cache bound to this model including it's own query cache (if enabled).

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

### guard

Use the `guard` method to manually re-enabled the mass assignment guard.

```php
$pages->guard();
```

### unguard

Use the `unguard` method to manually disabled the mass assignment guard.

```php
$pages->unguard();
```
