-
-
Save CROSP/de909251feaa1c29bfd39336bc5aa4db to your computer and use it in GitHub Desktop.
Base Laravel Repository Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Base Repository. | |
*/ | |
class Repository implements BaseRepository { | |
/** | |
* The model instance. | |
* | |
* @var \Illuminate\Database\Eloquent\Model | |
*/ | |
protected $model; | |
public function __construct( $model ) { | |
$this->model = $model; | |
} | |
/** | |
* Returns the first record in the database. | |
* | |
* @return \Illuminate\Database\Eloquent\Model | |
*/ | |
public function first() { | |
return $this->model->first(); | |
} | |
public function getTableName() { | |
return $this->model->getTable(); | |
} | |
/** | |
* Returns all the records. | |
* | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
public function all() { | |
return $this->model->all(); | |
} | |
/** | |
* Returns the count of all the records. | |
* | |
* @return int | |
*/ | |
public function count() { | |
return $this->model->count(); | |
} | |
public function countWhere( $attribute, $value ) { | |
return $this->model->where( $attribute, $value )->count(); | |
} | |
/** | |
* Returns a range of records bounded by pagination parameters. | |
* | |
* @param int $limit | |
* @param int $pageNumber | |
* @param array $relations | |
* @param string $orderBy | |
* @param string $sorting | |
* | |
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator | |
*/ | |
public function page( $limit = 10, $pageNumber = 1, $orderBy = 'id', $sorting = 'asc' ) { | |
return $this->model->orderBy( $orderBy, $sorting )->paginate( $limit, [ '*' ], 'page', $pageNumber ); | |
} | |
public function pageWhere( $limit = 10, $pageNumber = 0, $whereConditions = [], $orderBy = 'id', $sorting = 'asc' ) { | |
$query = $this->model->orderBy( $orderBy, $sorting ); | |
foreach ( $whereConditions as $condition ) { | |
$query = $query->where( ...$condition ); | |
} | |
return $query->paginate( $limit, [ '*' ], 'page', $pageNumber ); | |
} | |
public function pageWhereLike( $limit = 10, $pageNumber = 0, $whereLikeCols = [], $searchQuery = "", $orderBy = 'id', $sorting = 'asc' ) { | |
return $this->model->orderBy( $orderBy, $sorting )->whereLike( $whereLikeCols, $searchQuery )->paginate( $limit, [ '*' ], 'page', $pageNumber ); | |
} | |
public function pageWhereWhereLike( $limit = 10, $pageNumber = 0, $whereConditions = [], $whereLikeCols = [], $searchQuery = "", $orderBy = 'id', $sorting = 'asc' ) { | |
$query = $this->model->orderBy( $orderBy, $sorting ); | |
foreach ( $whereConditions as $condition ) { | |
$query = $query->where( ...$condition ); | |
} | |
return $query->whereLike( $whereLikeCols, $searchQuery )->paginate( $limit, [ '*' ], 'page', $pageNumber ); | |
} | |
/** | |
* Find a record by its identifier. | |
* | |
* @param string $id | |
* @param array $relations | |
* | |
* @return \Illuminate\Database\Eloquent\Model | |
*/ | |
public function find( $id, $relations = null ) { | |
return $this->findBy( $this->model->getKeyName(), $id, $relations ); | |
} | |
/** | |
* Find a record by an attribute. | |
* Fails if no model is found. | |
* | |
* @param string $attribute | |
* @param string $value | |
* @param array $relations | |
* | |
* @return \Illuminate\Database\Eloquent\Model | |
*/ | |
public function findBy( $attribute, $value, $relations = null ) { | |
$query = $this->model->where( $attribute, $value ); | |
if ( $relations && is_array( $relations ) ) { | |
foreach ( $relations as $relation ) { | |
$query->with( $relation ); | |
} | |
} | |
return $query->first(); | |
} | |
/** | |
* Get all records by an associative array of attributes. | |
* Two operators values are handled: AND | OR. | |
* | |
* @param array $attributes | |
* @param string $operator | |
* @param array $relations | |
* | |
* @return \Illuminate\Support\Collection | |
*/ | |
public function getByAttributes( array $attributes, $operator = 'AND', $relations = null ) { | |
// In the following it doesn't matter wivh element to start with, in all cases all attributes will be appended to the | |
// builder. | |
// Get the last value of the associative array | |
$lastValue = end( $attributes ); | |
// Get the last key of the associative array | |
$lastKey = key( $attributes ); | |
// Builder | |
$query = $this->model->where( $lastKey, $lastValue ); | |
// Pop the last key value pair of the associative array now that it has been added to Builder already | |
array_pop( $attributes ); | |
$method = 'where'; | |
if ( strtoupper( $operator ) === 'OR' ) { | |
$method = 'orWhere'; | |
} | |
foreach ( $attributes as $key => $value ) { | |
$query->$method( $key, $value ); | |
} | |
if ( $relations && is_array( $relations ) ) { | |
foreach ( $relations as $relation ) { | |
$query->with( $relation ); | |
} | |
} | |
return $query->get(); | |
} | |
/** | |
* Fills out an instance of the model | |
* with $attributes. | |
* | |
* @param array $attributes | |
* | |
* @return \Illuminate\Database\Eloquent\Model | |
*/ | |
public function fill( $attributes ) { | |
return $this->model->fill( $attributes ); | |
} | |
/** | |
* Fills out an instance of the model | |
* and saves it, pretty much like mass assignment. | |
* | |
* @param array $attributes | |
* | |
* @return \Illuminate\Database\Eloquent\Model | |
*/ | |
public function fillAndSave( $attributes ) { | |
$this->model->fill( $attributes ); | |
$this->model->save(); | |
return $this->model; | |
} | |
public function getQuery() { | |
return DB::table( $this->getTableName() ); | |
} | |
/** | |
* Remove a selected record. | |
* | |
* @param string $key | |
* | |
* @return bool | |
*/ | |
public function remove( $key ) { | |
return $this->model->where( $this->model->getKeyName(), $key )->delete(); | |
} | |
/** | |
* Implement a convenience call to findBy | |
* which allows finding by an attribute name | |
* as follows: findByName or findByAlias. | |
* | |
* @param string $method | |
* @param array $arguments | |
* | |
* @return mixed | |
*/ | |
public function __call( $method, $arguments ) { | |
/* | |
* findBy convenience calling to be available | |
* through findByName and findByTitle etc. | |
*/ | |
if ( preg_match( '/^findBy/', $method ) ) { | |
$attribute = strtolower( substr( $method, 6 ) ); | |
array_unshift( $arguments, $attribute ); | |
return call_user_func_array( array( $this, 'findBy' ), $arguments ); | |
} | |
} | |
public function getRawSqlStatement( $statement ) { | |
return DB::raw( $statement ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment