thirteen/repositories

Repository Pattern Provider.

0.3.1 2015-09-14 06:15 UTC

This package is not auto-updated.

Last update: 2020-08-07 19:49:43 UTC


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! :)