Created
February 13, 2016 02:22
-
-
Save owenconti/70fe5fe06b4fb84d7732 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Repositories; | |
use Illuminate\Container\Container as App; | |
use Illuminate\Database\Eloquent\Model; | |
/** | |
* Class Repository | |
* @package App\Repositories | |
*/ | |
abstract class Repository { | |
/** | |
* @var App | |
*/ | |
private $app; | |
/** | |
* @var | |
*/ | |
protected $model; | |
/** | |
* @param App $app | |
*/ | |
public function __construct(App $app) { | |
$this->app = $app; | |
$this->makeModel(); | |
} | |
/** | |
* Specify Model class name | |
* | |
* @return mixed | |
*/ | |
abstract function model(); | |
/** | |
* @param array $columns | |
* @return mixed | |
*/ | |
public function all($columns = array('*')) { | |
return $this->model->get($columns); | |
} | |
/** | |
* @param int $perPage | |
* @param array $columns | |
* @return mixed | |
*/ | |
public function paginate($perPage = 15, $columns = array('*')) { | |
return $this->model->paginate($perPage, $columns); | |
} | |
/** | |
* @param array $data | |
* @return mixed | |
*/ | |
public function create(array $data) { | |
$model = $this->app->make($this->model()); | |
return $model::create( $data ); | |
} | |
/** | |
* @param array $data | |
* @param $id | |
* @param string $attribute | |
* @return mixed | |
*/ | |
public function update(array $data, $id, $attribute="id") { | |
return $this->model->where($attribute, '=', $id)->update($data); | |
} | |
/** | |
* @param $id | |
* @return mixed | |
*/ | |
public function delete($id) { | |
return $this->model->where('id', '=', $id)->delete(); | |
} | |
/** | |
* @param $id | |
* @param array $columns | |
* @return mixed | |
*/ | |
public function find($id, $columns = array('*')) { | |
return $this->model->find($id, $columns); | |
} | |
/** | |
* @param $id | |
* @param array $columns | |
* @return mixed | |
*/ | |
public function findOrFail($id, $columns = array('*')) { | |
return $this->model->findOrFail($id, $columns); | |
} | |
/** | |
* @param $attribute | |
* @param $value | |
* @param array $columns | |
* @return mixed | |
*/ | |
public function findBy($attribute, $value, $columns = array('*')) { | |
return $this->model->where($attribute, '=', $value)->first($columns); | |
} | |
/** | |
* @param $id | |
* @return mixed | |
*/ | |
public function touch( $id ) { | |
$model = $this->findOrFail( $id ); | |
return $model->touch(); | |
} | |
/** | |
* @return \Illuminate\Database\Eloquent\Builder | |
* @throws Exception | |
*/ | |
public function makeModel() { | |
$model = $this->app->make($this->model()); | |
if (!$model instanceof Model) { | |
abort(500, "Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model"); | |
} | |
return $this->model = $model->newQuery(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment