Skip to content

Instantly share code, notes, and snippets.

@squareboat
Forked from akaamitgupta/ClientRepository.php
Created December 5, 2016 16:40
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 squareboat/7bac025b0449ba55d814df20571c8413 to your computer and use it in GitHub Desktop.
Save squareboat/7bac025b0449ba55d814df20571c8413 to your computer and use it in GitHub Desktop.
A Repository Implementation for Eloquent.
<?php
namespace App\Repositories\Eloquent;
use App\Models\Client;
use App\Traits\EloquentRepositoryTrait;
use App\Repositories\Contracts\ClientRepository as ClientRepositoryContract;
class ClientRepository implements ClientRepositoryContract
{
use EloquentRepositoryTrait;
/**
* The model associated with the repository.
*
* @var App\Models\Client
*/
protected $model = Client::class;
}
<?php
namespace App\Traits;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model as EloquentModel;
trait EloquentRepositoryTrait
{
/**
* Get all of the models from the database.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all()
{
return $this->query()->get();
}
/**
* Get the paginated models from the database.
*
* @param int $perPage
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = 15)
{
return $this->query()->paginate($perPage);
}
/**
* Get a model by its primary key.
*
* @param int $id
* @return \Illuminate\Database\Eloquent\Model
*
* @throws \App\Exceptions\ModelNotFoundException
*/
public function get($id)
{
$model = $this->query()->find($id);
return $model;
}
/**
* Get models by the value.
*
* @param int $id
* @param array $moreWhere
* @return \Illuminate\Database\Eloquent\Collection
*
* @throws \App\Exceptions\ModelNotFoundException
*/
public function getWhere($column, $value, array $moreWhere = null)
{
$query = $this->query()->where($column, $value);
if (! is_null($moreWhere)) {
$query->where($moreWhere);
}
$models = $query->get();
if ($models->isEmpty()) throw new ModelNotFoundException(sprintf('No %s record found.', get_classname($this->model)));
return $models;
}
/**
* Get the model data by adding the given query
*
* @param string $column
* @param mixed $value
* @param array $moreWhere
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getWhereIn($column, $value, array $moreWhere = null)
{
$query = $this->query()->whereIn($column, $value);
if (! is_null($moreWhere)) {
$query->where($moreWhere);
}
$models = $query->get();
if ($models->isEmpty()) throw new ModelNotFoundException(sprintf('No %s record found.', get_classname($this->model)));
return $models;
}
/**
* Check if any relation exists.
*
* @param int $id
* @param array $relations
* @return bool
*/
public function hasRelations($id, array $relations)
{
if (count($relations) == 0) {
throw new \InvalidArgumentException('The hasRelations function only accepts non empty array of relations.');
}
$query = $this->query()->where('id', $id);
$query->where(function($q) use($relations) {
foreach ($relations as $relation) {
$q->orHas($relation);
}
});
return $query->exists();
}
/**
* Save a new model and return the instance.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $attributes)
{
$modelName = $this->model;
return $modelName::create($attributes);
}
/**
* Get the first record matching the attributes or instantiate it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
{
return $this->query()->firstOrNew($attributes);
}
/**
* Get the first record matching the attributes or create it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes)
{
return $this->query()->firstOrCreate($attributes);
}
/**
* Update the model by the given attributes.
*
* @param \Illuminate\Database\Eloquent\Model|int $model
* @return bool|int
*/
public function update($model, $attributes)
{
return ($model instanceof EloquentModel) ? $model->update($attributes) : $this->get($model)->update($attributes);
}
/**
* Delete the model from the database.
*
* @param \Illuminate\Database\Eloquent\Model|int $model
* @return bool|null
*
* @throws \Exception
*/
public function delete($model)
{
return ($model instanceof EloquentModel) ? $model->delete() : $this->get($model)->delete();
}
/**
* Begin querying the model.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function query()
{
return call_user_func("{$this->model}::query");
}
}
<?php
/**
* Get the class name from the full class path.
*
* @param string $name
* @return string
*/
function get_classname($name)
{
return str_replace('_', ' ', snake_case(class_basename($name)));
}
<?php
namespace App\Exceptions;
use Illuminate\Database\Eloquent\ModelNotFoundException as Exception;
class ModelNotFoundException extends Exception
{
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment