zatoday/repository 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zatoday/repository

Composer 安装命令:

composer require zatoday/repository

包简介

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

README 文档

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.

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固