Skip to content

Instantly share code, notes, and snippets.

@muratsplat
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muratsplat/dd1efb3735caf7f8c6c0 to your computer and use it in GitHub Desktop.
Save muratsplat/dd1efb3735caf7f8c6c0 to your computer and use it in GitHub Desktop.
A abstract class for repository
<?php namespace Boruu\Root\Mvc\Repository;
/**
* Core Repository for Eloquent
*
* @author Murat Ödünç <murat.asya@gmail.com>
* @copyright (c) 2015, Murat Ödünç
* @license http://www.gnu.org/licenses/gpl-3.0.html GPLv3
* @package Repository
*/
abstract class EloquentRepository {
/**
* Eloquent Model
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $model;
/**
* current model
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $current;
/**
* Constructer
*/
public function __construct($model = null) {
$this->model = $model;
}
/**
* to get all users in a collection object
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all() {
return $this->model->all();
}
/**
* to get model by id
*
* @param int $id
* @return Illuminate\Database\Eloquent\Model|null
*/
public function getById($id) {
return $this->model->find($id);
}
/**
* To create model
*
* @param array $attributes
* @return bool
*/
abstract public function create(array $attributes);
/**
* To update model
*
* @param int $id
* @param array $attributes
* @return boolean
*/
abstract public function update($id, array $attributes);
/**
* to detete model
*
* @param int $id
* @return bool
*/
public function delete($id) {
$this->current = $this->getById($id);
return is_null($this->current) ? false : $this->current->delete();
}
/**
* To get all users in Collection object
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getModels() {
return $this->all();
}
/**
* to get Instance
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function getCurrent() {
return $this->current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment