dugajean / repositories
Laravel Repositories
Installs: 865
Dependents: 0
Suggesters: 0
Security: 0
Stars: 66
Watchers: 3
Forks: 17
Open Issues: 4
Requires
- php: ^7.2
- illuminate/console: ^5.4|^6.0
- illuminate/database: ^5.4|^6.0
- illuminate/filesystem: ^5.4|^6.0
- illuminate/support: ^5.4|^6.0
Requires (Dev)
- mockery/mockery: @stable
- phpunit/phpunit: ^8.3
This package is auto-updated.
Last update: 2022-10-08 18:42:46 UTC
README
Laravel Repositories is a package for Laravel 5 which is used to abstract the database layer. This makes applications much easier to maintain.
This package was originally created by Bosnadev, who is no longer maintaining it; therefore, I have decided to take this project over and assure its maintenance.
Installation
Run the following command from you terminal:
composer require dugajean/repositories
Usage
First, create your repository class with this command:
php artisan make:repository Film
Where Film
is the name of an existing model. If the model does not exist, it will be generated for you.
Finally, use the repository in the controller:
<?php namespace App\Http\Controllers; use App\Repositories\FilmRepository; class FilmsController extends Controller { /** * @var FilmRepository */ private $filmRepository; public function __construct(FilmRepository $filmRepository) { $this->filmRepository = $filmRepository; } public function index() { return response()->json($this->filmRepository->all()); } }
Publishing The Configuration
If you wish to override the path where the repositories and criteria live, publish the config file:
php artisan vendor:publish --provider="Dugajean\Repositories\Providers\RepositoryProvider"
Then simply open config/repositories.php
and edit away!
Available Methods
The following methods are available:
Dugajean\Repositories\Contracts\RepositoryInterface
public function all($columns = ['*']) public function lists($value, $key = null) public function paginate($perPage = 1, $columns = ['*'], $method = 'full'); public function create(array $data) // if you use mongodb then you'll need to specify primary key $attribute public function update(array $data, $id, $attribute = 'id') public function delete($id) public function find($id, $columns = ['*']) public function findBy($field, $value, $columns = ['*']) public function findAllBy($field, $value, $columns = ['*']) public function findWhere($where, $columns = ['*'])
Dugajean\Repositories\Contracts\CriteriaInterface
public function apply($model, Repository $repository)
Example usage
Create a new film in repository:
$this->filmRepository->create(Input::all());
Update existing film:
$this->filmRepository->update(Input::all(), $film_id);
Delete film:
$this->filmRepository->delete($id);
Find film by film_id;
$this->filmRepository->find($id);
you can also chose what columns to fetch:
$this->filmRepository->find($id, ['title', 'description', 'release_date']);
Get a single row by a single column criteria.
$this->filmRepository->findBy('title', $title);
Or you can get all rows by a single column criteria.
$this->filmRepository->findAllBy('author_id', $author_id);
Get all results by multiple fields
$this->filmRepository->findWhere([ 'author_id' => $author_id, ['year', '>', $year] ]);
Criteria
Criteria is a simple way to apply specific condition, or set of conditions to the repository query.
To create a Criteria class, run the following command:
php artisan make:criteria LengthOverTwoHours --model=Film
Here is a sample criteria:
<?php namespace App\Repositories\Criteria\Films; use Dugajean\Repositories\Criteria\Criteria; use Dugajean\Repositories\Contracts\RepositoryInterface; class LengthOverTwoHours extends Criteria { /** * @param $model * @param RepositoryInterface $repository * * @return Model */ public function apply($model, RepositoryInterface $repository) { return $model->where('length', '>', 120); } }
Now, inside you controller class you call pushCriteria method:
<?php namespace App\Http\Controllers; use App\Repositories\FilmRepository; use App\Repositories\Criteria\Films\LengthOverTwoHours; class FilmsController extends Controller { /** * @var FilmRepository */ private $filmRepository; public function __construct(FilmRepository $filmRepository) { $this->filmRepository = $filmRepository; } public function index() { $this->filmRepository->pushCriteria(new LengthOverTwoHours()); return response()->json($this->filmRepository->all()); } }
Testing
$ vendor/bin/phpunit
License
Pouch is released under the MIT License.