Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created March 5, 2016 05:44
Show Gist options
  • Save vinicius73/1baa2beaee4cd80f6c9c to your computer and use it in GitHub Desktop.
Save vinicius73/1baa2beaee4cd80f6c9c to your computer and use it in GitHub Desktop.
Base Repository for Laravel Eloquent
<?php
namespace App\Support\Repositories;
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\AbstractPaginator as Paginator;
abstract class BaseRepository
{
/**
* Model class for repo.
*
* @var string
*/
protected $modelClass;
/**
* @return EloquentQueryBuilder|QueryBuilder
*/
protected function newQuery()
{
return app($this->modelClass)->newQuery();
}
/**
* @param EloquentQueryBuilder|QueryBuilder $query
* @param int $take
* @param bool $paginate
*
* @return EloquentCollection|Paginator
*/
protected function doQuery($query = null, $take = 15, $paginate = true)
{
if (is_null($query)) {
$query = $this->newQuery();
}
if (true == $paginate) {
return $query->paginate($take);
}
if ($take > 0 || false !== $take) {
$query->take($take);
}
return $query->get();
}
/**
* Returns all records.
* If $take is false then brings all records
* If $paginate is true returns Paginator instance.
*
* @param int $take
* @param bool $paginate
*
* @return EloquentCollection|Paginator
*/
public function getAll($take = 15, $paginate = true)
{
return $this->doQuery(null, $take, $paginate);
}
/**
* @param string $column
* @param string|null $key
*
* @return \Illuminate\Support\Collection
*/
public function lists($column, $key = null)
{
return $this->newQuery()->lists($column, $key);
}
/**
* Retrieves a record by his id
* If fail is true $ fires ModelNotFoundException.
*
* @param int $id
* @param bool $fail
*
* @return Model
*/
public function findByID($id, $fail = true)
{
if ($fail) {
return $this->newQuery()->findOrFail($id);
}
return $this->newQuery()->find($id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment