Skip to content

Instantly share code, notes, and snippets.

@adibhanna
Created May 13, 2020 01:02
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 adibhanna/26829cd8d0f949ab1a01af408678f3cd to your computer and use it in GitHub Desktop.
Save adibhanna/26829cd8d0f949ab1a01af408678f3cd to your computer and use it in GitHub Desktop.
Repository
<?php
namespace App\Repositories;
class Repository
{
/**
* 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();
}
/**
* Returns all the records.
*
* @return Illuminate\Database\Eloquent\Collection
*/
public function all()
{
return $this->model->all();
}
/**
* Find a record by its identifier.
*
* @param string $id
* @return Illuminate\Database\Eloquent\Model
*/
public function find($id)
{
return $this->model->findOrFail($id);
}
/**
* Find a record by an attribute.
*
* @param string $attribute
* @param string $value
* @param array $relations
* @return Illuminate\Database\Eloquent\Model
*/
public function findBy($attribute, $value, $relations = null)
{
if ($relations and is_array($relations)) {
$query = $this->model->where($attribute, $value);
foreach ($relations as $relation) {
$query->with($relation);
}
return $query->firstOrFail();
}
return $this->model->where($attribute, $value)->firstOrFail();
}
/**
* 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);
}
public function findByIdOrSlug($id_or_slug)
{
try {
return $this->find($id_or_slug);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return $this->findBy('slug', $id_or_slug);
}
}
/**
* 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;
}
/**
* Remove a selected record.
*
* @param string $key
* @return boolean
*/
public function remove($key)
{
return $this->model->where($this->model->getKeyName(), $key)->delete();
}
/**
* determine if the $identifier is an ID or a Slug
*
* @param $identifier
*
* @return string
*/
public function isIdOrSlug($identifier)
{
if (is_numeric($identifier)) {
return 'id';
} else {
return 'slug';
}
}
/**
* 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([$this, 'findBy'], $arguments);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment