此包已被弃用且不再维护。未建议替代包。

轻松在Laravel中生成Repository模式

v1.1.21 2017-06-29 08:52 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

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

待办事项

清理代码

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件