Skip to content

Instantly share code, notes, and snippets.

@carbontwelve
Last active May 30, 2018 11:20
Show Gist options
  • Save carbontwelve/2e5a04b46db06629ce785162eeac8021 to your computer and use it in GitHub Desktop.
Save carbontwelve/2e5a04b46db06629ce785162eeac8021 to your computer and use it in GitHub Desktop.
Repository pattern for woogle
<?php namespace Photogabble\Database;
use Illuminate\Database\Eloquent\Model;
class EloquentRepository
{
/** @var \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder */
protected $model;
public function __construct($model = null)
{
$this->model = $model;
}
/**
* Model getter
* @return Model|null
*/
public function getModel()
{
return $this->model;
}
/**
* Model setter
* @param Model $model
*/
public function setModel($model)
{
$this->model = $model;
}
/**
* Return all records in this collection
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function findAll()
{
return $this->model->all();
}
public function findRandomRow()
{
return $this->model->whereRaw("id > FLOOR( RAND() * (SELECT MAX(id) FROM ". $this->model->getTable() ." ))")->first();
}
/**
* @param int $count
* @return mixed
*/
public function getAllPaginated($count = 10)
{
return $this->model->paginate($count);
}
/**
* @param int $id
* @return \Illuminate\Database\Eloquent\Collection|Model|static
*/
public function findById($id)
{
return $this->model->find($id);
}
/**
* @param array $ids
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByIds(array $ids)
{
return $this->model->whereIn('id', $ids)->get();
}
/**
* @param $id
* @return \Illuminate\Database\Eloquent\Collection|Model|static
*/
public function findByParentId($id)
{
return $this->model->where('parent_id', $id)->get();
}
/**
* Returns a count of all records in the collection
* @return int
*/
public function countAll()
{
return $this->model->all()->count();
}
/**
* @param array $attributes
* @return Model|static
*/
public function getNew($attributes = array())
{
return $this->model->newInstance($attributes);
}
/**
* @param array|\Illuminate\Database\Eloquent\Model $data
* @return mixed
*/
public function save($data)
{
if ($data instanceOf Model) {
return $this->storeEloquentModel($data);
} elseif (is_array($data)) {
return $this->storeArray($data);
}
return false;
}
/**
* @param Model $model
* @return mixed
*/
public function delete($model)
{
return $model->delete();
}
/**
* @param Model $model
* @return mixed
*/
protected function storeEloquentModel($model)
{
if ($model->getDirty()) {
return $model->save();
} else {
return $model->touch();
}
}
/**
* @param array $data
* @return mixed
*/
protected function storeArray($data)
{
$model = $this->getNew($data);
return $this->storeEloquentModel($model);
}
}
<?php namespace Photogabble\Controllers;
class SomeController
{
private $userRepository;
public function __construct(Photogabble\Database\UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function someFunction($id)
{
if (! $user = $this->userRepository->findById($id)) {
// ...
}
// ...
}
public function someOtherFunction()
{
$model = $this->userRepository->getNew(['name' => 'hello']);
if (! $this->userRepository->save($model)) {
// ...
}
}
public function onceAgainWithFeeling($id)
{
$model = $this->userRepository->getNew();
if (! $user = $model->whereId($id)->first()) {
// ...
}
}
}
<?php namespace Photogabble\Database;
use \App\Models\User;
class UserRepository extends EloquentRepository
{
public function __construct(User $model)
{
parent::__construct($model);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment