zatoday / repository by ZAToday

package repository for laravel, all function eloquent, Eager Loading and upgrade make model
32
2
2
Package Data
Maintainer Username: ZAToday
Maintainer Contact: za@zatoday.com (ZA)
Package Create Date: 2017-09-09
Package Last Update: 2017-11-26
Home Page: https://zatoday.com
Language: PHP
License: MIT
Last Refreshed: 2024-12-23 03:07:14
Package Statistics
Total Downloads: 32
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

ZAToday Repository

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

  • [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:

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.