Laravel Repository Pattern: A Full Base Repository Example for Laravel 4 | Kyle Noland

The repository pattern is gaining popularity in the PHP community these days. Here is a Base Repository class you can use to build your own implementation.

BaseRepository.php
PHP

<?php

abstract class BaseRepository implements BaseRepositoryInterface
{
/**
* The repository model
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $model;

/**
* The query builder
*
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $query;

/**
* Alias for the query limit
*
* @var int
*/
protected $take;

/**
* Array of related models to eager load
*
* @var array
*/
protected $with = array();

/**
* Array of one or more where clause parameters
*
* @var array
*/
protected $wheres = array();

/**
* Array of one or more where in clause parameters
*
* @var array
*/
protected $whereIns = array();

/**
* Get all the model records in the database
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all()
{
$this->newQuery()->eagerLoad();

$models = $this->query->get();

$this->unsetClauses();

return $models;
}

/**
* Count the number of specified model records in the database
*
* @return int
*/
public function count()
{
return $this->get()->count();
}

/**
* Create a new model record in the database
*
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $data)
{
$this->unsetClauses();

return $this->model->create($data);
}

/**
* Delete one or more model records from the database
*
* @return mixed
*/
public function delete()
{
$this->newQuery()->setClauses();

$result = $this->query->delete();

$this->unsetClauses();

return $result;
}

/**
* Delete the specified model record from the database
*
* @param $id
*
* @return bool|null
* @throws \Exception
*/
public function deleteById($id)
{
$this->unsetClauses();

return $this->getById($id)->delete();
}

public function deleteMultipleById(array $ids)
{
return $this->model->destroy($ids);
}

/**
* Get the first specified model record from the database
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function first()
{
$this->newQuery()->eagerLoad()->setClauses();

$model = $this->query->firstOrFail();

$this->unsetClauses();

return $model;
}

/**
* Get all the specified model records in the database
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function get()
{
$this->newQuery()->eagerLoad()->setClauses();

$models = $this->query->get();

$this->unsetClauses();

return $models;
}

/**
* Get the specified model record from the database
*
* @param $id
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function getById($id)
{
$this->unsetClauses();

$this->newQuery()->eagerLoad();

return $this->query->findOrFail($id);
}

/**
* Set the query limit
*
* @param int $limit
*
* @return $this
*/
public function limit($limit)
{
$this->take = $limit;

return $this;
}

/**
* Update the specified model record in the database
*
* @param $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function updateById($id, array $data)
{
$this->unsetClauses();

$model = $this->getById($id);

$model->update($data);

return $model;
}

/**
* Add a simple where clause to the query
*
* @param string $column
* @param string $value
* @param string $operator
*
* @return $this
*/
public function where($column, $value, $operator = ‘=’)
{
$this->wheres[] = compact(‘column’, ‘value’, ‘operator’);

return $this;
}

/**
* Add a simple where in clause to the query
*
* @param string $column
* @param mixed $values
*
* @return $this
*/
public function whereIn($column, $values)
{
$values = is_array($values) ? $values : array($values);

$this->whereIns[] = compact(‘column’, ‘values’);

return $this;
}

/**
* Set Eloquent relationships to eager load
*
* @param $relations
*
* @return $this
*/
public function with($relations)
{
if (is_string($relations))
{
$relations = func_get_args();
}

$this->with = $relations;

return $this;
}

/**
* Create a new instance of the model’s query builder
*
* @return $this
*/
protected function newQuery()
{
$this->query = $this->model->newQuery();

return $this;
}

/**
* Add relationships to the query builder to eager load
*
* @return $this
*/
protected function eagerLoad()
{
foreach($this->with as $relation)
{
$this->query->with($relation);
}

return $this;
}

/**
* Set clauses on the query builder
*
* @return $this
*/
protected function setClauses()
{
foreach($this->wheres as $where)
{
$this->query->where($where[‘column’], $where[‘operator’], $where[‘value’]);
}

foreach($this->whereIns as $whereIn)
{
$this->query->whereIn($whereIn[‘column’], $whereIn[‘values’]);
}

if(isset($this->take) and ! is_null($this->take))
{
$this->query->take($this->take);
}

return $this;
}

/**
* Reset the query clause parameter arrays
*
* @return $this
*/
protected function unsetClauses()
{
$this->wheres = array();
$this->whereIns = array();
$this->take = null;

return $this;
}
}

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 <?phpabstract class BaseRepository implements BaseRepositoryInterface{ /** * The repository model * * @var \Illuminate\Database\Eloquent\Model */ protected $model; /** * The query builder * * @var \Illuminate\Database\Eloquent\Builder */ protected $query; /** * Alias for the query limit * * @var int */ protected $take; /** * Array of related models to eager load * * @var array */ protected $with = array(); /** * Array of one or more where clause parameters * * @var array */ protected $wheres = array(); /** * Array of one or more where in clause parameters * * @var array */ protected $whereIns = array(); /** * Get all the model records in the database * * @return \Illuminate\Database\Eloquent\Collection */ public function all() { $this->newQuery()->eagerLoad(); $models = $this->query->get(); $this->unsetClauses(); return $models; } /** * Count the number of specified model records in the database * * @return int */ public function count() { return $this->get()->count(); } /** * Create a new model record in the database * * @param array $data * * @return \Illuminate\Database\Eloquent\Model */ public function create(array $data) { $this->unsetClauses(); return $this->model->create($data); } /** * Delete one or more model records from the database * * @return mixed */ public function delete() { $this->newQuery()->setClauses(); $result = $this->query->delete(); $this->unsetClauses(); return $result; } /** * Delete the specified model record from the database * * @param $id * * @return bool|null * @throws \Exception */ public function deleteById($id) { $this->unsetClauses(); return $this->getById($id)->delete(); } public function deleteMultipleById(array $ids) { return $this->model->destroy($ids); } /** * Get the first specified model record from the database * * @return \Illuminate\Database\Eloquent\Model */ public function first() { $this->newQuery()->eagerLoad()->setClauses(); $model = $this->query->firstOrFail(); $this->unsetClauses(); return $model; } /** * Get all the specified model records in the database * * @return \Illuminate\Database\Eloquent\Collection */ public function get() { $this->newQuery()->eagerLoad()->setClauses(); $models = $this->query->get(); $this->unsetClauses(); return $models; } /** * Get the specified model record from the database * * @param $id * * @return \Illuminate\Database\Eloquent\Model */ public function getById($id) { $this->unsetClauses(); $this->newQuery()->eagerLoad(); return $this->query->findOrFail($id); } /** * Set the query limit * * @param int $limit * * @return $this */ public function limit($limit) { $this->take = $limit; return $this; } /** * Update the specified model record in the database * * @param       $id * @param array $data * * @return \Illuminate\Database\Eloquent\Model */ public function updateById($id, array $data) { $this->unsetClauses(); $model = $this->getById($id); $model->update($data); return $model; } /** * Add a simple where clause to the query * * @param string $column * @param string $value * @param string $operator * * @return $this */ public function where($column, $value, $operator = ‘=’) { $this->wheres[] = compact(‘column’, ‘value’, ‘operator’); return $this; } /** * Add a simple where in clause to the query * * @param string $column * @param mixed  $values * * @return $this */ public function whereIn($column, $values) { $values = is_array($values) ? $values : array($values); $this->whereIns[] = compact(‘column’, ‘values’); return $this; } /** * Set Eloquent relationships to eager load * * @param $relations * * @return $this */ public function with($relations) { if (is_string($relations)) {     $relations = func_get_args(); } $this->with = $relations; return $this; } /** * Create a new instance of the model’s query builder * * @return $this */ protected function newQuery() { $this->query = $this->model->newQuery(); return $this; } /** * Add relationships to the query builder to eager load * * @return $this */ protected function eagerLoad() { foreach($this->with as $relation) { $this->query->with($relation); } return $this; } /** * Set clauses on the query builder * * @return $this */ protected function setClauses() { foreach($this->wheres as $where) { $this->query->where($where[‘column’], $where[‘operator’], $where[‘value’]); } foreach($this->whereIns as $whereIn) { $this->query->whereIn($whereIn[‘column’], $whereIn[‘values’]); } if(isset($this->take) and ! is_null($this->take)) { $this->query->take($this->take); } return $this; } /** * Reset the query clause parameter arrays * * @return $this */ protected function unsetClauses() { $this->wheres = array(); $this->whereIns = array(); $this->take = null; return $this; }}