Git Product home page Git Product logo

repository's Introduction

Laravel Repositories

Build Status SensioLabsInsight Latest Stable Version Total Downloads Monthly Downloads License

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

Installation

Run the following command from you terminal:

composer require "bosnadev/repositories: 0.*"

or add this to require section in your composer.json file:

"bosnadev/repositories": "0.*"

then run composer update

Usage

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

<?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:

<?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:

<?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:

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:

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

Update existing film:

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

Delete film:

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

Find film by film_id;

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

you can also chose what columns to fetch:

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

Get a single row by a single column criteria.

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

Or you can get all rows by a single column criteria.

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

Get all results by multiple fields

$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.

Here is a simple criteria:

<?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:

<?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.

repository's People

Contributors

mirzap avatar maartenpaauw avatar jdsingh avatar casperlaitw avatar miclf avatar petehouston avatar xcaptain avatar thangngoc89 avatar wink- avatar willianmano avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.