thirteen/repositories 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thirteen/repositories

最新稳定版本:0.3.1

Composer 安装命令:

composer require thirteen/repositories

包简介

Repository Pattern Provider.

README 文档

README

Repositories is a package to easily setup the repository pattern in your laravel application.

Installation

Laravel 5.0

composer require "thirteen/repositories:0.1.*"

Laravel 5.1

composer require "thirteen/repositories:0.2.*"

Update your providers in your app.php

Thirteen\Repositories\RepositoriesServiceProvider::class

Usage

You can extend your repositories with the eloquent repository to get all the basic CRUD functions.

See Practices for easier maintenance.

To use the methods, just extend from the Abstract Repository and implement the model() method.

App\Repositories\UserRepository

namespace App\Repositories;

use App\Contracts\Repositories\UserRepsository as UserRepsositoryInterface;

class UserRepository extends Repository implements UserRepositoryInterface
{
	public function model()
	{
		return \App\User::class;
	}
}

And there you go, an eloquent repository without the need to creating the basic functions from scratch every single time.

Methods

Extend an interface from the package which will implement all the standard eloquent features:

  • create(array $attributes)
  • all(array $columns = ['*'])
  • find($id, $columns = ['*'])
  • update($id, $columns = ['*'])
  • destroy($ids)

Along with all that, you also get a build function, to easily explain, it's to make saving relations easier.

$attributes = ['title' => 'First Post'];

$user = $userRepository->find(1);
$post = $postRepository->build($attributes);

$user->posts()->save($post);

Or you can pass in a nested array of attributes for saving many.

$attributes = [
	['title' => 'First Post'],
	['title' => 'Second Post'],
];

$user = $userRepository->find(1);
$posts = $postRepository->build($attributes);

$user->posts()->saveMany($posts);

Practices

I highly recommend extending the interface and the repository to your own abstract one so you still have control over all the repositories.

Using this method, you don't have to extend all your classes as well. So it's less of a hassle this way.

App\Contracts\Repositories\RepositoryInterface

namespace App\Contracts\Repositories;

use Thirteen\Repositories\Contracts\RepositoryInterface as ThirteenRepository;

interface Repository extends ThirteenRepository
{
}

App\Repositories\Repository

namespace App\Repositories;

use Thirteen\Repositories\Eloquent\Repository as ThirteenRepository;

abstract class Repository extends ThirteenRepository
{
}

This way, you can still add your own custom commands that will be applied to all your child repositories.

Todo

  • Create a paginate method.
  • Rename the RepositoryInterface to Repository

Contributing

If you want to contribute, feel free to fork the project and leave pull requests. Thanks! :)

统计信息

  • 总下载量: 173
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-12