Last active
February 19, 2018 11:30
-
-
Save sydgren/f9a981c7944818d701540c9d99bfc9c2 to your computer and use it in GitHub Desktop.
Laravel repositories
This file contains hidden or 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 Repository | |
{ | |
public function all($columns = array('*')); | |
public function paginate($perPage = 15, $columns = array('*')); | |
public function create(array $data); | |
public function update(array $data, $id); | |
public function delete($id); | |
public function find($id, $columns = array('*')); | |
public function findBy($field, $value, $columns = array('*')); | |
} |
This file contains hidden or 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; | |
use App\Models\User; | |
/** | |
* Some Repository | |
* | |
* @mixin App\Repositories\Contracts\Repository | |
*/ | |
interface SomeRepository | |
{ | |
public function byUser(User $user); | |
} |
This file contains hidden or 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\Eloquent; | |
use App\Models\User; | |
use App\Repositories\Repository; | |
use App\Repositories\Contracts\SomeRepository as SomeRepositoryContract; | |
class SomeRepository extends Repository implements SomeRepositoryContract | |
{ | |
protected function model() | |
{ | |
return App\Models\SomeModel::class; | |
} | |
public function byUser(User $user) | |
{ | |
$this->model->whereUserId($user->id)->get(); | |
} | |
} |
This file contains hidden or 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\Exceptions; | |
class RepositoryException extends \Exception | |
{ | |
// | |
} |
This file contains hidden or 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\Database\Eloquent\Model; | |
abstract class Repository implements Contracts\Repository | |
{ | |
protected $model; | |
/** | |
* @throws Exceptions\RepositoryException | |
*/ | |
public function __construct(App $app) | |
{ | |
$this->makeModel(); | |
} | |
abstract protected function model(); | |
public function all($columns = array('*')) | |
{ | |
return $this->model->get($columns); | |
} | |
public function paginate($perPage = 15, $columns = array('*')) | |
{ | |
return $this->model->paginate($perPage, $columns); | |
} | |
public function create(array $data) | |
{ | |
return $this->model->create($data); | |
} | |
public function update(array $data, $id, $attribute = "id") | |
{ | |
return $this->model->where($attribute, '=', $id)->update($data); | |
} | |
public function delete($id) | |
{ | |
return $this->model->destroy($id); | |
} | |
public function find($id, $columns = array('*')) | |
{ | |
return $this->model->find($id, $columns); | |
} | |
public function findBy($attribute, $value, $columns = array('*')) | |
{ | |
return $this->model->where($attribute, '=', $value)->first($columns); | |
} | |
protected function makeModel() | |
{ | |
$model = resolve($this->model()); | |
if (! $model instanceof Model) | |
throw new Exceptions\RepositoryException("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