2017 © Pedro Peláez
 

library repository

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

image

zatoday/repository

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

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

ZAToday Repository

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

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

Feature

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

Installation

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

bash composer require zatoday/repository, (*4)

Usage

Create new file Repository bash php artisan make:repository [options] [--] <name>, (*5)

Example:, (*6)

// 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:, (*7)

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class User extends Model {


}

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

<?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:, (*9)

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:, (*10)

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

Update existing film:, (*11)

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

Delete film:, (*12)

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

Find film by film_id;, (*13)

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

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

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

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

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

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

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

Get all results by multiple fields, (*17)

$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., (*18)

Create new file Criteria: bash php artisan make:criteria, (*19)

Example:, (*20)

// Create Criteria

php artisan make:criteria UserMaxId

// Or create Criteria with folder User

php artisan make:criteria User\MaxId

Here is a simple criteria:, (*21)

<?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:, (*22)

<?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, (*23)

<?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., (*24)

The Versions

26/11 2017

v1.2.1

1.2.1.0 https://zatoday.com

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository model repositories laravel5 laravel5.5

21/11 2017

dev-master

9999999-dev https://zatoday.com

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository model repositories laravel5 laravel5.5

21/11 2017

v1.2.0

1.2.0.0 https://zatoday.com

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository model repositories laravel5 laravel5.5

12/09 2017

v1.1.3

1.1.3.0 https://zatoday.com

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository model repositories laravel5 laravel5.5

12/09 2017

v1.0.1

1.0.1.0 https://zatoday.com

package repository for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository repositories laravel5 laravel5.5

12/09 2017

v1.1.2

1.1.2.0 https://zatoday.com

package repository for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository repositories laravel5 laravel5.5

12/09 2017

v1.0.0

1.0.0.0 https://zatoday.com

package repository for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository repositories laravel5 laravel5.5

09/09 2017

v1.1.1

1.1.1.0 https://zatoday.com

package repository for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository repositories laravel5 laravel5.5

09/09 2017

v1.1

1.1.0.0 https://zatoday.com

package repository for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository repositories laravel5 laravel5.5

09/09 2017

v1.0

1.0.0.0 https://zatoday.com

package repository for laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by ZA

laravel repository repositories laravel5 laravel5.5