Package Data | |
---|---|
Maintainer Username: | garethnic |
Maintainer Contact: | gareth.nic@io.co.za (Gareth Nicholson) |
Package Create Date: | 2016-07-05 |
Package Last Update: | 2016-07-19 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:10:12 |
Package Statistics | |
---|---|
Total Downloads: | 22 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This package creates scaffolding to implement the Repository pattern.
composer require io-digital/repo
Add the ServiceProvider to your config/app.php providers array:
IoDigital\Repo\RepoServiceProvider::class,
Then run the following artisan command:
$ php artisan vendor:publish --provider="IoDigital\Repo\RepoServiceProvider"
This will create the following folder structure in your app/
folder:
After installing the package the artisan command repo:create
should be available.
To create the repository structure for your object run the command:
$ php artisan repo:create Post
This will create the following files:
It will also add the bindings in your AppServiceProvider.php
file:
$this->app->bind(
'App\Models\Contracts\Repositories\PostRepository', // Repository (Interface)
'App\Models\Concrete\Eloquent\EloquentPostRepository' // Eloquent (Class)
);
Then in your Controller it's simply:
...
use App\Models\Contracts\Repositories\PostRepository;
...
protected $model;
public function __construct(PostRepository $repo)
{
$this->model = $repo;
}
-m
or --m
Use the the -m
option to create a migration file for your object:
$ php artisan repo:create Post -m
The repository interface provides the following methods:
public function all($with = [], $orderBy = [], $columns = ['*']);
public function find($id, $relations = []);
public function findBy($attribute, $value, $columns = ['*']);
public function findAllBy($attribute, $value, $columns = ['*']);
public function findWhere($where, $columns = ['*'], $or = false);
public function findWhereIn($field, array $values, $columns = ['*']);
public function paginate($perPage = 25, $columns = ['*']);
public function simplePaginate($limit = null, $columns = ['*']);
public function create($attributes = []);
public function edit($id, $attributes = []);
public function delete($id);
The implementations can found in Models/Concrete/AbstractEloquentRepository.php
Example usage for the find functions:
//returns with ->first()
$data = $this->model->findBy('title', $title);
//returns with ->get()
$data = $this->model->findAllBy('category', $category);
//returns with ->get()
$data = $this->model->findWhere([
'category' => $category,
['year', '>' , $year],
['name', 'like', "%$name%"],
['surname', 'like', '%nes']
]);
$data = $this->model->findWhereIn('id', [1, 2, 3])
->get(['name', 'email']);
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
Clean up code
The MIT License (MIT). Please see License File for more information.