io-digital / repo
轻松在Laravel中生成Repository模式
Requires
- php: ~5.6|~7.0
- illuminate/support: ~5.1
Requires (Dev)
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.3
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- dev-master / 1.0.x-dev
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-feature/package-autodiscovery
- dev-feature/controller-creation
- dev-feature/update-repo-functions
- dev-feature/where-in
- dev-feature/usage-update
- dev-feature/progress-bar
This package is not auto-updated.
Last update: 2021-05-09 14:48:59 UTC
README
此包创建脚手架以实现Repository模式。
安装
Repo支持Laravel的包自动发现
composer require io-digital/repo
将ServiceProvider添加到config/app.php的providers数组中
IoDigital\Repo\RepoServiceProvider::class,
然后运行以下Artisan命令
$ php artisan vendor:publish --provider="IoDigital\Repo\RepoServiceProvider"
这将创建以下文件夹结构在您的app/文件夹中
- 模型
- 具体
- Eloquent
- _AbstractEloquentRepository.php
- 合同
- 存储库
- _RepositoryInterface.php
- 对象
- 具体
注意,_AbstractEloquentRepository.php和_RepositoryInterface.php被命名为这样以避免覆盖现有文件。在全新安装的情况下,这些文件可以简单地重命名为AbstractEloquentRepository.php和RepositoryInterface.php,分别。如果这些文件已经存在,请手动将新发布的文件与现有文件合并。
使用方法
安装包后,应该可以使用Artisan命令repo:create。
要为您的对象创建存储库结构,请运行以下命令
$ php artisan repo:create Post
这将创建以下文件
- 模型/对象/Post.php
- 模型/合同/存储库/PostRepository.php
- 模型/具体/Eloquent/EloquentPostRepository.php
它还会在您的AppServiceProvider.php文件中添加绑定
$this->app->bind( 'App\Models\Contracts\Repositories\PostRepository', // Repository (Interface) 'App\Models\Concrete\Eloquent\EloquentPostRepository' // Eloquent (Class) );
然后在您的控制器中,它是这样的
... use App\Models\Contracts\Repositories\PostRepository; ... protected $model; public function __construct(PostRepository $repo) { $this->model = $repo; }
选项
-m或--m-c或--c
使用-m选项为您的对象创建迁移文件
$ php artisan repo:create Post -m
使用-c选项为您的对象创建资源控制器
$ php artisan repo:create Post -c
全部一起
$ php artisan repo:create Post -m -c
存储库接口提供以下方法
public function make($with = [], $orderBy = []); 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);
实现可以在模型/具体/AbstractEloquentRepository.php中找到
查找函数的示例用法
//returns the model with the relationship as a paginated collection $data = $this->model->make(['relation'])->paginate(); //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']);
变更日志
有关最近更改的信息,请参阅变更日志
测试
$ composer test
贡献
请参阅CONTRIBUTING和CONDUCT以获取详细信息。
待办事项
清理代码
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件