|
1 | 1 | # Laravel Repositories
|
| 2 | + |
2 | 3 | Laravel Repositories to abstract the database layer.
|
| 4 | + |
| 5 | +##Installation |
| 6 | + |
| 7 | +Run the following command to install the latest version |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require "freevital/laravel-repository" |
| 11 | +``` |
| 12 | + |
| 13 | +##Usage |
| 14 | + |
| 15 | +###Create a Repository |
| 16 | + |
| 17 | +Your repository class must extend `Freevital\Repository\Eloquent\BaseRepository` abstract class and implement method `model()` which returns model's class name. |
| 18 | + |
| 19 | +```php |
| 20 | +namespace App\Repositories\Eloquent; |
| 21 | + |
| 22 | +use Freevital\Repository\Eloquent\BaseRepository; |
| 23 | + |
| 24 | +class PostRepository extends BaseRepository |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Specify Model class name. |
| 28 | + * |
| 29 | + * @return string |
| 30 | + */ |
| 31 | + public function model() |
| 32 | + { |
| 33 | + return "App\Post"; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +###Use the repository in the controller |
| 39 | + |
| 40 | +```php |
| 41 | +namespace App\Http\Controllers; |
| 42 | + |
| 43 | +use App\Http\Controllers\Controller; |
| 44 | +use App\Repositories\Criteria\BySlugCriteria; |
| 45 | +use App\Repositories\Criteria\WithCommentsCriteria; |
| 46 | +use App\Repositories\Eloquent\PostRepository; |
| 47 | + |
| 48 | +class PostController extends Controller |
| 49 | +{ |
| 50 | + /** |
| 51 | + * @var PostRepository |
| 52 | + */ |
| 53 | + protected $postRepository; |
| 54 | + |
| 55 | + /** |
| 56 | + * @param PostRepository $postRepository |
| 57 | + */ |
| 58 | + public function __construct(PostRepository $postRepository) |
| 59 | + { |
| 60 | + $this->postRepository = $postRepository; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get all posts. |
| 65 | + * |
| 66 | + * @return \Illuminate\Http\JsonResponse |
| 67 | + */ |
| 68 | + public function index() |
| 69 | + { |
| 70 | + $posts = $this->postRepository->all(); |
| 71 | + |
| 72 | + return \Response::json(compact('posts')); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +###Create a Repository Criteria |
| 78 | + |
| 79 | +Optionally you may create a separate Criteria class to apply specific query conditions. Your Criteria class must implement `Freevital\Repository\Contracts\CriteriaContract` interface. |
| 80 | + |
| 81 | +```php |
| 82 | +namespace App\Repositories\Criteria; |
| 83 | + |
| 84 | +use Illuminate\Database\Eloquent\Builder; |
| 85 | +use Freevital\Repository\Contracts\CriteriaContract; |
| 86 | +use Freevital\Repository\Contracts\RepositoryContract; |
| 87 | + |
| 88 | +class BySlugCriteria implements CriteriaContract |
| 89 | +{ |
| 90 | + /** |
| 91 | + * @var string |
| 92 | + */ |
| 93 | + protected $title; |
| 94 | + |
| 95 | + /** |
| 96 | + * @param string $slug |
| 97 | + */ |
| 98 | + public function __construct($slug) |
| 99 | + { |
| 100 | + $this->slug = $slug; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Apply criteria in query repository. |
| 105 | + * |
| 106 | + * @param Builder $query |
| 107 | + * @param RepositoryContract $repository |
| 108 | + * |
| 109 | + * @return \Illuminate\Database\Eloquent\Builder |
| 110 | + */ |
| 111 | + public function apply(Builder $query, RepositoryContract $repository) |
| 112 | + { |
| 113 | + return $query->where('name', $this->slug); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +###Use Repository Criteria in the controller |
| 119 | + |
| 120 | +You may use multiple criteria in a repository. |
| 121 | + |
| 122 | +```php |
| 123 | +namespace App\Http\Controllers; |
| 124 | + |
| 125 | +use App\Http\Controllers\Controller; |
| 126 | +use App\Repositories\Criteria\BySlugCriteria; |
| 127 | +use App\Repositories\Criteria\WithCommentsCriteria; |
| 128 | +use App\Repositories\Eloquent\PostRepository; |
| 129 | + |
| 130 | +class PostController extends Controller |
| 131 | +{ |
| 132 | + /** |
| 133 | + * Get a post by slug. |
| 134 | + * |
| 135 | + * @param string $slug |
| 136 | + * |
| 137 | + * @return \Illuminate\Http\JsonResponse |
| 138 | + */ |
| 139 | + public function show($slug) |
| 140 | + { |
| 141 | + $post = $this->postRepository |
| 142 | + ->pushCriteria(new WithCommentsCriteria($slug)) |
| 143 | + ->pushCriteria(new BySlugCriteria($slug)) |
| 144 | + ->first(); |
| 145 | + |
| 146 | + return \Response::json(compact('post')); |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +##Available Methods |
| 152 | + |
| 153 | +###Freevital\Repository\Contracts\RepositoryContract |
| 154 | + |
| 155 | +```php |
| 156 | +paginate($limit = null, $columns = ['*'], $method = 'paginate') |
| 157 | +simplePaginate($limit = null, $columns = ['*']) |
| 158 | +all($columns = ['*']) |
| 159 | +lists($column, $key = null) |
| 160 | +find($id, $columns = ['*']) |
| 161 | +first($columns = ['*']) |
| 162 | +findByField($field, $value, $columns = ['*']) |
| 163 | +findWhere(array $where, $columns = ['*']) |
| 164 | +findWhereIn($field, array $values, $columns = ['*']) |
| 165 | +findWhereNotIn($field, array $values, $columns = ['*']) |
| 166 | +count() |
| 167 | +create(array $attributes) |
| 168 | +update(array $attributes, $id) |
| 169 | +updateOrCreate(array $attributes, array $values = []) |
| 170 | +updateActiveStatus($status, int $id) |
| 171 | +delete($id) |
| 172 | +forceDelete($id) |
| 173 | +deleteWhere(array $where) |
| 174 | +forceDeleteWhere(array $where) |
| 175 | +has($relation) |
| 176 | +with($relations) |
| 177 | +whereHas($relation, $closure) |
| 178 | +orderBy($column, $direction = 'asc') |
| 179 | +visible(array $fields) |
| 180 | +hidden(array $fields) |
| 181 | +scopeQuery(\Closure $scope) |
| 182 | +resetScope() |
| 183 | +``` |
| 184 | + |
| 185 | +###Freevital\Repository\Contracts\RepositoryCriteriaContract |
| 186 | + |
| 187 | +```php |
| 188 | +pushCriteria($criteria) |
| 189 | +popCriteria($criteria) |
| 190 | +getCriteria() |
| 191 | +getByCriteria(CriteriaContract $criteria) |
| 192 | +skipCriteria($status = true) |
| 193 | +resetCriteria() |
| 194 | +``` |
| 195 | + |
| 196 | +###Freevital\Repository\Contracts\CriteriaContract |
| 197 | + |
| 198 | +```php |
| 199 | +apply(Builder $query, RepositoryContract $repository) |
| 200 | +``` |
| 201 | + |
| 202 | +##Credits |
| 203 | + |
| 204 | +This package in mainly based on package by [@andersao](https://github.com/andersao/l5-repository). |
| 205 | + |
| 206 | +##License |
| 207 | + |
| 208 | +The contents of this repository is released under the MIT license. |
0 commit comments