zatoday/repository

package repository for laravel, all function eloquent, Eager Loading and upgrade make model

v1.2.1 2017-11-26 05:24 UTC

This package is not auto-updated.

Last update: 2024-04-14 00:59:48 UTC


README

Latest Stable Version Build Status License Total Downloads Monthly Downloads

ZAToday Repository is a package for Laravel 5.5 which is used to abstract the database layer. This makes applications much easier to maintain.

Feature

  • all($columns = array('*'))
  • lists($value, $key = null)
  • paginate($perPage = 1, $columns = array('*'));
  • create(array $data)
  • update(array $data, $id, $attribute = "id") // if you use mongodb then you'll need to specify primary key $attribute
  • delete($id)
  • find($id, $columns = array('*'))
  • findBy($field, $value, $columns = array('*'))
  • findAllBy($field, $value, $columns = array('*'))
  • findWhere($where, $columns = array('*')) // Eager Loading
  • with($relations)
  • Custom make:model

Installation

Run the following command from you terminal:

composer require zatoday/repository

Usage

Create new file Repository

php artisan make:repository [options] [--] <name>

Example:

// Create Repository is User

php artisan make:repository User

// Or create Repository is User and create model User

php artisan make:repository -m User
<?php


namespace App\Repositories;


use ZAToday\Repository\Repository;


class User extends Repository {

    public function model() {
        return \App\User::class;
    }
}

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

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class User extends Model {


}

And finally, use the repository in the controller:

<?php


namespace App\Http\Controllers;


use App\Repositories\User;


class UserController extends Controller {

    private $user;

    public function __construct(User $user) {

        $this->user = $user;
    }

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

Available Methods

The following methods are available:

ZAToday\Repository\Contracts\RepositoryInterface
public function all($columns = array('*'))
public function - [x] lists($value, $key = null)
public function - [x] paginate($perPage = 1, $columns = array('*'));
public function - [x] create(array $data)
public function - [x] update(array $data, $id, $attribute = "id")
// if you use mongodb then you'll need to specify primary key $attribute
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('*'))
ZAToday\Repository\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 ZAToday\Repository\Criteria class.

Create new file Criteria:

php artisan make:criteria

Example:

// Create Criteria

php artisan make:criteria UserMaxId

// Or create Criteria with folder User

php artisan make:criteria User\MaxId

Here is a simple criteria:

<?php

namespace App\Repositories\Criteria;

use ZAToday\Repository\Criteria;
use ZAToday\Repository\Contracts\RepositoryInterface as Repository;

class UserMaxId extends Criteria {

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

Now, inside you controller class you call pushCriteria method:

<?php

namespace App\Http\Controllers;

use App\Repositories\Criteria\UserMaxId;
use App\Repositories\User;

class UserController extends Controller {

    /**
     * @var User
     */
    private $user;

    public function __construct(User $user) {

        $this->user = $user;
    }

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

or

<?php

namespace App\Http\Controllers;

use App\Repositories\Criteria\UserMaxId;
use App\Repositories\User;

class UserController extends Controller {

    /**
     * @var User
     */
    private $user;

    public function __construct(User $user) {

        $this->user = $user;
    }

    public function index() {
        $criteria = new UserMaxId
        return \Response::json($this->user->getByCriteria($criteria)->all());
    }
}

Credits

This package is largely inspired by this great package by @Bosnadev.