Repositories
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 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 following documentation will assume you have a scenario like this:
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface;
$pages = app(PageRepositoryInterface::class);
return $pages->find($id);
all
The all method returns a collection of all entries.
$pages->all();
allWithTrashed
The allWithTrashed method returns a collection of all entries including trashed ones.
$pages->allWithTrashed();
allWithoutRelations
The allWithoutRelations method returns a collection of all entries without relations.
$pages->allWithoutRelations();
find
The find method returns a single entry by id.
$pages->find($id);
findBy
The findBy method returns a single entry where column is value.
$pages->findBy($column, $value);
For
entrymodels thecolumncould also be afield_slug{.tip}
findAll
The findAll method returns a collection of entries in $ids.
$pages->findAll(array $ids);
findAll
The findAllBy method returns a collection of entries where column is value.
$pages->findAllBy($column, $value);
For
entrymodels thecolumncould also be afield_slug{.tip}
findTrashed
The findTrashed method returns a collection of all trashed entries.
$pages->findTrashed();
create
The create method creates a new entry with attributes.
$pages->create(array $attributes);
newQuery
The newQuery method returns a new query builder instance for the contained model.
$pages->newQuery()->where('path', 'about/company')->first();
newInstance
The newInstance method returns a new model/entry instance optionally with attributes.
$newPage = $pages->newInstance(array $attributes = []);
count
The count method returns the total number of entries in the database.
$pages->count();
paginate
The paginate method returns a paginator instance from parameters.
$pages->paginate(array $parameters = []);
You can create an optional simple paginator with parameters like this:
$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.
$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.
$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.
$pages->update(['enabled' => true]);
delete
Use the update method to delete an entry.
$pages->delete($page);
forceDelete
Use the forceDelete method to delete an entry and bypass trashing if trashable.
$pages->forceDelete($page);
restore
Use the restore method to restore a trashed entry.
$pages->restore($page);
truncate
Use the truncate method to truncate the entry's database table.
$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.
$pages->cache($key, $ttl, $value);
This can be used much like using Laravel's cache directly.
$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.
$pages->cacheForever($key, $value);
This can be used much like using Laravel's cache directly.
$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).
$pages->flushCache();
guard
Use the guard method to manually re-enabled the mass assignment guard.
$pages->guard();
unguard
Use the unguard method to manually disabled the mass assignment guard.
$pages->unguard();