Last active
February 2, 2017 07:06
-
-
Save warrickbayman/ed5028051b84217a8edf to your computer and use it in GitHub Desktop.
Simple Laravel 5 repositories (PHP 7+)
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 App\Repositories\Contracts\RepositoryInterface; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\Model; | |
/** | |
* Class UserRepository | |
* | |
* @package App\Repositories | |
*/ | |
class Repository implements RepositoryInterface | |
{ | |
/** | |
* @var Model|Builder | |
*/ | |
protected $model; | |
/** | |
* @var array | |
*/ | |
protected $with; | |
/** | |
* Apply the eager load models to the query | |
* | |
* @param $query | |
* | |
* @return mixed | |
*/ | |
private function queryWith($query) | |
{ | |
if ($this->with) { | |
foreach ($this->with as $with) { | |
$query = $query->with($with); | |
} | |
} | |
return $query; | |
} | |
/** | |
* Eager load the models in the array | |
* | |
* @param array $with | |
* | |
* @return $this | |
*/ | |
public function with(array $with) | |
{ | |
if (!$this->with) { | |
$this->with = $with; | |
} else { | |
$this->with = array_merge($this->with, $with); | |
} | |
return $this; | |
} | |
/** | |
* Get all records | |
* | |
* @param array $columns | |
* @param string|null $orderBy | |
* @param string $orderDirection | |
* | |
* @return mixed | |
*/ | |
public function all(array $columns = ['*'], string $orderBy = null, string $orderDirection = 'asc') | |
{ | |
$query = $this->queryWith($this->model); | |
if ($orderBy) { | |
return $query->orderBy($orderBy, $orderDirection)->get($columns); | |
} | |
return $query->get($columns); | |
} | |
/** | |
* Paginate all records | |
* | |
* @param int $perPage | |
* @param array $columns | |
* @param string|null $orderBy | |
* @param string $orderDirection | |
* | |
* @return mixed | |
*/ | |
public function paginate( | |
int $perPage = 20, | |
array $columns = ['*'], | |
string $orderBy = null, | |
string $orderDirection = 'asc' | |
) { | |
$query = $this->queryWith($this->model); | |
if ($orderBy) { | |
$query = $query->orderBy($orderBy, $orderDirection); | |
} | |
return $query->paginate($perPage, $columns); | |
} | |
/** | |
* Find a record by its ID | |
* | |
* @param int $id | |
* @param array $columns | |
* | |
* @return mixed | |
*/ | |
public function find(int $id, array $columns = ['*']) | |
{ | |
return $this->queryWith($this->model)->find($id, $columns); | |
} | |
/** | |
* Find a record by the specified key and value | |
* | |
* @param string $key | |
* @param mixed $value | |
* @param array $columns | |
* | |
* @return mixed | |
*/ | |
public function findBy(string $key, $value, array $columns = ['*']) | |
{ | |
return $this->queryWith($this->model)->where($key, '=', $value)->first($columns); | |
} | |
/** | |
* Create a new record | |
* | |
* @param array $properties | |
* @param bool $nullZeroLength | |
* | |
* @return mixed | |
*/ | |
public function create(array $properties, bool $nullZeroLength = false) | |
{ | |
if ($nullZeroLength) { | |
foreach (array_keys($properties) as $property) { | |
if (strlen($properties[$property]) === 0) { | |
$properties[$property] = null; | |
} | |
} | |
} | |
return $this->model->create($properties); | |
} | |
/** | |
* Update a record by its ID | |
* | |
* @param int $id | |
* @param array $properties | |
* @param bool $nullZeroLength | |
* | |
* @return mixed | |
*/ | |
public function update(int $id, array $properties, bool $nullZeroLength = false) | |
{ | |
if ($nullZeroLength) { | |
foreach (array_keys($properties) as $property) { | |
if (strlen($properties[$property]) === 0) { | |
$properties[$property] = null; | |
} | |
} | |
} | |
$entity = $this->model->find($id); | |
$entity->update($properties); | |
return $entity; | |
} | |
/** | |
* Delete a record | |
* | |
* @param int $id | |
* @param bool $force | |
* | |
* @return void | |
*/ | |
public function delete(int $id, bool $force = false) | |
{ | |
if ($force) { | |
$this->model->find($id)->forceDelete(); | |
} | |
$this->model->find($id)->delete(); | |
} | |
} |
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\Contracts; | |
/** | |
* Interface RepositoryInterface | |
* | |
* @package App\Repositories\Contracts | |
*/ | |
interface RepositoryInterface | |
{ | |
/** | |
* Eager load the models in the array | |
* | |
* @param array $with | |
* | |
* @return $this | |
*/ | |
public function with(array $with); | |
/** | |
* Get all records | |
* | |
* @param array $columns | |
* @param string|null $orderBy | |
* @param string $orderDirection | |
* | |
* @return mixed | |
*/ | |
public function all(array $columns = ['*'], string $orderBy = null, string $orderDirection = 'asc'); | |
/** | |
* Paginate all records | |
* | |
* @param int $perPage | |
* @param array $columns | |
* @param string|null $orderBy | |
* @param string $orderDirection | |
* | |
* @return mixed | |
*/ | |
public function paginate( | |
int $perPage = 20, | |
array $columns = ['*'], | |
string $orderBy = null, | |
string $orderDirection = 'asc' | |
); | |
/** | |
* Find a record by its ID | |
* | |
* @param int $id | |
* @param array $columns | |
* | |
* @return mixed | |
*/ | |
public function find(int $id, array $columns = ['*']); | |
/** | |
* Find a record by the specified key and value | |
* | |
* @param string $key | |
* @param mixed $value | |
* @param array $columns | |
* | |
* @return mixed | |
*/ | |
public function findBy(string $key, $value, array $columns = ['*']); | |
/** | |
* Create a new record | |
* | |
* @param array $properties | |
* @param bool $nullZeroLength | |
* | |
* @return mixed | |
*/ | |
public function create(array $properties, bool $nullZeroLength = false); | |
/** | |
* Update a record by its ID | |
* | |
* @param int $id | |
* @param array $properties | |
* @param bool $nullZeroLength | |
* | |
* @return mixed | |
*/ | |
public function update(int $id, array $properties, bool $nullZeroLength = false); | |
/** | |
* Delete a record | |
* | |
* @param int $id | |
* @param bool $force | |
* | |
* @return mixed | |
*/ | |
public function delete(int $id, bool $force = false); | |
} |
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 App\Repositories\Contracts\UserRepositoryInterface; | |
use App\User; | |
/** | |
* Class UserRepository | |
* | |
* @package App\Repositories | |
*/ | |
class UserRepository extends Repository implements UserRepositoryInterface | |
{ | |
/** | |
* UserRepository constructor. | |
* | |
* @param User $model | |
*/ | |
public function __construct(User $model) | |
{ | |
$this->model = $model; | |
$this->with = [ | |
App\Posts::class | |
]; | |
} | |
/** | |
* Find a user by their email address | |
* | |
* @param string $email | |
*/ | |
public function findByEmail(string $email) | |
{ | |
return $this->findBy('email', $email); | |
} | |
} |
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\Contracts; | |
/** | |
* Interface UserRepositoryInterface | |
* | |
* @package App\Repositories\Contracts | |
*/ | |
interface UserRepositoryInterface extends RepositoryInterface | |
{ | |
/** | |
* Find a user by their email address | |
* | |
* @param string $email | |
*/ | |
public function findByEmail(string $email); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment