2017 © Pedro Peláez
 

library repositories

Laravel Repositories

image

bosnadev/repositories

Laravel Repositories

  • Sunday, September 18, 2016
  • by mirzap
  • Repository
  • 57 Watchers
  • 704 Stars
  • 242,165 Installations
  • PHP
  • 12 Dependents
  • 0 Suggesters
  • 158 Forks
  • 44 Open issues
  • 15 Versions
  • 8 % Grown

The README.md

Laravel Repositories

Build Status SensioLabsInsight Latest Stable Version Total Downloads Monthly Downloads License, (*1)

Laravel Repositories is a package for Laravel 5 which is used to abstract the database layer. This makes applications much easier to maintain., (*2)

Installation

Run the following command from you terminal:, (*3)

bash composer require "bosnadev/repositories: 0.*", (*4)

or add this to require section in your composer.json file:, (*5)

"bosnadev/repositories": "0.*", (*6)

then run composer update, (*7)

Usage

First, create your repository class. Note that your repository class MUST extend Bosnadev\Repositories\Eloquent\Repository and implement model() method, (*8)

<?php namespace App\Repositories;

use Bosnadev\Repositories\Contracts\RepositoryInterface;
use Bosnadev\Repositories\Eloquent\Repository;

class FilmsRepository extends Repository {

    public function model() {
        return 'App\Film';
    }
}

By implementing model() method you telling repository what model class you want to use. Now, create App\Film model:, (*9)

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Film extends Model {

    protected $primaryKey = 'film_id';

    protected $table = 'film';

    protected $casts = [
        "rental_rate"       => 'float'
    ];
}

And finally, use the repository in the controller:, (*10)

<?php namespace App\Http\Controllers;

use App\Repositories\FilmsRepository as Film;

class FilmsController extends Controller {

    private $film;

    public function __construct(Film $film) {

        $this->film = $film;
    }

    public function index() {
        return \Response::json($this->film->all());
    }
}

Available Methods

The following methods are available:, (*11)

Bosnadev\Repositories\Contracts\RepositoryInterface
public function all($columns = array('*'))
public function lists($value, $key = null)
public function paginate($perPage = 1, $columns = array('*'));
public function create(array $data)
// if you use mongodb then you'll need to specify primary key $attribute
public function update(array $data, $id, $attribute = "id")
public function delete($id)
public function find($id, $columns = array('*'))
public function findBy($field, $value, $columns = array('*'))
public function findAllBy($field, $value, $columns = array('*'))
public function findWhere($where, $columns = array('*'))
Bosnadev\Repositories\Contracts\CriteriaInterface
public function apply($model, Repository $repository)

Example usage

Create a new film in repository:, (*12)

$this->film->create(Input::all());

Update existing film:, (*13)

$this->film->update(Input::all(), $film_id);

Delete film:, (*14)

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

Find film by film_id;, (*15)

$this->film->find($id);

you can also chose what columns to fetch:, (*16)

$this->film->find($id, ['title', 'description', 'release_date']);

Get a single row by a single column criteria., (*17)

$this->film->findBy('title', $title);

Or you can get all rows by a single column criteria., (*18)

$this->film->findAllBy('author_id', $author_id);

Get all results by multiple fields, (*19)

$this->film->findWhere([
    'author_id' => $author_id,
    ['year','>',$year]
]);

Criteria

Criteria is a simple way to apply specific condition, or set of conditions to the repository query. Your criteria class MUST extend the abstract Bosnadev\Repositories\Criteria\Criteria class., (*20)

Here is a simple criteria:, (*21)

<?php namespace App\Repositories\Criteria\Films;

use Bosnadev\Repositories\Criteria\Criteria;
use Bosnadev\Repositories\Contracts\RepositoryInterface as Repository;

class LengthOverTwoHours extends Criteria {

    /**
     * @param $model
     * @param RepositoryInterface $repository
     * @return mixed
     */
    public function apply($model, Repository $repository)
    {
        $model = $model->where('length', '>', 120);
        return $model;
    }
}

Now, inside you controller class you call pushCriteria method:, (*22)

<?php namespace App\Http\Controllers;

use App\Repositories\Criteria\Films\LengthOverTwoHours;
use App\Repositories\FilmsRepository as Film;

class FilmsController extends Controller {

    /**
     * @var Film
     */
    private $film;

    public function __construct(Film $film) {

        $this->film = $film;
    }

    public function index() {
        $this->film->pushCriteria(new LengthOverTwoHours());
        return \Response::json($this->film->all());
    }
}

Credits

This package is largely inspired by this great package by @andersao. Here is another package I used as reference., (*23)

The Versions

18/09 2016

dev-master

9999999-dev https://bosnadev.com

Laravel Repositories

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

22/08 2016

dev-develop

dev-develop https://bosnadev.com

Laravel Repositories

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

19/08 2016

0.13

0.13.0.0 https://bosnadev.com

Laravel Repositories

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

08/04 2016

0.12

0.12.0.0

Laravel Repositories

  Sources   Download

The Requires

 

The Development Requires

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

04/04 2016

0.11

0.11.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

24/03 2016

0.10

0.10.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

19/08 2015

0.9

0.9.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Maarten Paauw
by Mirza Pasic

database laravel repository eloquent repositories

20/04 2015

0.8

0.8.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

25/03 2015

0.7

0.7.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

17/03 2015

0.6

0.6.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

08/03 2015

0.5

0.5.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

06/03 2015

0.4

0.4.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

06/03 2015

0.3

0.3.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

02/03 2015

0.2

0.2.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories

02/03 2015

0.1

0.1.0.0

Laravel Repositories

  Sources   Download

The Requires

 

by Mirza Pasic

database laravel repository eloquent repositories