2017 © Pedro Peláez
 

library repository

image

gridprinciples/repository

  • Wednesday, November 18, 2015
  • by gbrock
  • Repository
  • 2 Watchers
  • 0 Stars
  • 125 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 2 % Grown

The README.md

Repository Build Status

A basic Eloquent Repository for Laravel 5.1., (*1)

Installation

  1. Run composer require gridprinciples/repository from your project directory.
  2. Add the following to the providers array in config/app.php:, (*2)

    GridPrinciples\Repository\RepositoryServiceProvider::class,
    
  3. Make a Repositories folder somewhere in your application, such as app/Repositories., (*3)

Usage

  1. Make a new Repository by extending GridPrinciples\Repository:, (*4)

    <?php
    
    namespace App\Repositories;
    
    use GridPrinciples\EloquentRepository;
    
    class FooRepository extends EloquentRepository {
        protected static $model = \App\Foo::class;
    }
    
    
  2. (Recommended) Use your repository in your controller(s):, (*5)

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Repositories\FooRepository;
    
    public function __construct(FooRepository $repository)
    {
        $this->repository = $repository;
    }
    
    public function somePage($id)
    {
        $model = $this->repository->get($id);
    
        if(!$model) {
            // Model not found.
            return abort(404);
        }
    
        return view('my_view', [
            'foo' => $model,
        ]);
    }
    
    

Some basic CRUD functionality is included with the EloquentRepository:, (*6)

Creating

You can call save with an array of data in order to make a new model/record., (*7)

$newModel = $this->repository->save([
    'title' => 'This is indicative of a title',
    'description' => 'You might have a description field, perhaps.',
]);

It is recommended you populate your model's $fillable array in order to avoid mass-assignment problems., (*8)

Reading

You can select one or many records by their keys (usually id) using get:, (*9)

$singleModel = $this->repository->get(1);
$multipleModels = $this->repository->get([2, 3, 4]);

If you'd like to retrieve many models and paginate them, use the index method:, (*10)

$pageOfModels = $this->repository->index(10); // 10 records per page

Updating

You can update models in a very similar way as creating, also by using the save method:, (*11)

$data = [
    'status' => 'active',
];
$id = 1;

$this->repository->save($data, $id);

You can also pass an array of keys as the second argument to save in order to update many records at once., (*12)

Deleting

Deleting models can be accomplished easily using the delete method:, (*13)

$this->repository->delete($id);

You can also pass an array of keys to delete in order to delete many records at once., (*14)

License

This is open-sourced software licensed under the MIT license., (*15)

The Versions

18/11 2015

dev-master

9999999-dev

  Sources   Download

The Development Requires

18/11 2015

0.1.1

0.1.1.0

  Sources   Download

The Development Requires

12/11 2015

0.1.0

0.1.0.0

  Sources   Download

The Development Requires