garethnic / repo
在Laravel中构建仓库模式
v0.1.10
2016-07-19 12:43 UTC
Requires
- php: ~5.5|~7.0
- illuminate/support: ~5.1
Requires (Dev)
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.3
This package is not auto-updated.
Last update: 2024-09-14 18:36:47 UTC
README
此包创建脚手架以实现仓库模式。
安装
composer require io-digital/repo
将服务提供者添加到您的config/app.php providers数组中
IoDigital\Repo\RepoServiceProvider::class,
然后运行以下artisan命令
$ php artisan vendor:publish --provider="IoDigital\Repo\RepoServiceProvider"
这将创建以下文件夹结构在您的app/文件夹中
- 模型
- 具体
- Eloquent
- AbstractEloquentRepository.php
- 合约
- 仓库
- RepositoryInterface.php
- 对象
- 具体
用法
安装包后,artisan命令repo:create应该可用。
要为您的对象创建仓库结构,请运行以下命令
$ php artisan repo:create Post
这将创建以下文件
- Models/Objects/Post.php
- Models/Contracts/Repositories/PostRepository.php
- Models/Concrete/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
使用-m选项为您对象创建迁移文件
$ php artisan repo:create Post -m
仓库接口提供以下方法
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);
实现可以在Models/Concrete/AbstractEloquentRepository.php中找到
find函数的示例用法
//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']);
变更日志
请参阅CHANGELOG以获取更多最近更改的信息。
测试
$ composer test
贡献
请参阅CONTRIBUTING和CONDUCT以获取详细信息。
待办事项
清理代码
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。