recca0120/repository

Laravel 5的仓库

v2.6.2 2023-02-15 12:39 UTC

README

StyleCI Build Status Total Downloads Latest Stable Version Latest Unstable Version License Monthly Downloads Daily Downloads Scrutinizer Code Quality Code Coverage

安装

要获取Laravel异常的最新版本,只需使用Composer引入项目

composer require recca0120/repository

当然,您也可以手动更新require块并运行composer update,如果您选择这样做

{
    "require": {
        "recca0120/repository": "~2.0.0"
    }
}

方法

Recca0120\Repository\EloquentRepository

  • find($id, $columns = ['*']);
  • findMany($ids, $columns = ['*']);
  • findOrFail($id, $columns = ['*']);
  • findOrNew($id, $columns = ['*']);
  • firstOrNew(array $attributes, array $values = []);
  • firstOrCreate(array $attributes, array $values = []);
  • updateOrCreate(array $attributes, array $values = []);
  • firstOrFail($criteria = [], $columns = ['*']);
  • create($attributes);
  • forceCreate($attributes);
  • update($id, $attributes);
  • forceUpdate($id, $attributes);
  • delete($id);
  • forceDelete($id);
  • newInstance($attributes = [], $exists = false);
  • get($criteria = [], $columns = ['*']);
  • chunk($criteria, $count, callable $callback);
  • each($criteria, callable $callback, $count = 1000);
  • first($criteria = [], $columns = ['*']);
  • paginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
  • simplePaginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
  • count($criteria = [], $columns = '*');
  • min($criteria, $column);
  • max($criteria, $column);
  • sum($criteria, $column);
  • avg($criteria, $column);
  • average($criteria, $column);
  • matching($criteria);
  • getQuery($criteria = []);
  • getModel();
  • newQuery();

Recca0120\Repository\Criteria

  • static create()
  • static expr($value)
  • static raw($value)
  • select($columns = ['*'])
  • selectRaw($expression, array $bindings = [])
  • selectSub($query, $as)
  • addSelect($column)
  • distinct()
  • from($table)
  • join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
  • joinWhere($table, $first, $operator, $second, $type = 'inner')
  • leftJoin($table, $first, $operator = null, $second = null)
  • leftJoinWhere($table, $first, $operator, $second)
  • rightJoin($table, $first, $operator = null, $second = null)
  • rightJoinWhere($table, $first, $operator, $second)
  • crossJoin($table, $first = null, $operator = null, $second = null)
  • mergeWheres($wheres, $bindings)
  • tap($callback)
  • where($column, $operator = null, $value = null, $boolean = 'and')
  • orWhere($column, $operator = null, $value = null)
  • whereColumn($first, $operator = null, $second = null, $boolean = 'and')
  • orWhereColumn($first, $operator = null, $second = null)
  • whereRaw($sql, $bindings = [], $boolean = 'and')
  • orWhereRaw($sql, array $bindings = [])
  • whereIn($column, $values, $boolean = 'and', $not = false)
  • orWhereIn($column, $values)
  • whereNotIn($column, $values, $boolean = 'and')
  • orWhereNotIn($column, $values)
  • whereNull($column, $boolean = 'and', $not = false)
  • orWhereNull($column)
  • whereNotNull($column, $boolean = 'and')
  • whereBetween($column, array $values, $boolean = 'and', $not = false)
  • orWhereBetween($column, array $values)
  • whereNotBetween($column, array $values, $boolean = 'and')
  • orWhereNotBetween($column, array $values)
  • orWhereNotNull($column)
  • whereDate($column, $operator, $value = null, $boolean = 'and')
  • orWhereDate($column, $operator, $value)
  • whereTime($column, $operator, $value, $boolean = 'and')
  • orWhereTime($column, $operator, $value)
  • whereDay($column, $operator, $value = null, $boolean = 'and')
  • whereMonth($column, $operator, $value = null, $boolean = 'and')
  • whereYear($column, $operator, $value = null, $boolean = 'and')
  • whereNested(Closure $callback, $boolean = 'and')
  • addNestedWhereQuery($query, $boolean = 'and')
  • whereExists(Closure $callback, $boolean = 'and', $not = false)
  • orWhereExists(Closure $callback, $not = false)
  • whereNotExists(Closure $callback, $boolean = 'and')
  • orWhereNotExists(Closure $callback)
  • addWhereExistsQuery(Builder $query, $boolean = 'and', $not = false)
  • dynamicWhere($method, $parameters)
  • groupBy()
  • having($column, $operator = null, $value = null, $boolean = 'and')
  • orHaving($column, $operator = null, $value = null)
  • havingRaw($sql, array $bindings = [], $boolean = 'and')
  • orHavingRaw($sql, array $bindings = [])
  • orderBy($column, $direction = 'asc')
  • orderByDesc($column)
  • latest($column = 'created_at')
  • oldest($column = 'created_at')
  • inRandomOrder($seed = '')
  • orderByRaw($sql, $bindings = [])
  • skip($value)
  • offset($value)
  • take($value)
  • limit($value)
  • forPage($page, $perPage = 15)
  • forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
  • union($query, $all = false)
  • unionAll($query)
  • lock($value = true)
  • lockForUpdate()
  • sharedLock()
  • when($value, $callback, $default = null)
  • unless($value, $callback, $default = null)
  • whereKey($id)
  • whereKeyNot($id)
  • with($relations)
  • without($relations)
  • setQuery($query)
  • setModel(Model $model)
  • has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
  • orHas($relation, $operator = '>=', $count = 1)
  • doesntHave($relation, $boolean = 'and', Closure $callback = null)
  • whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1)
  • orWhereHas($relation, Closure $callback = null, $operator = '>=', $count = 1)
  • whereDoesntHave($relation, Closure $callback = null)
  • withCount($relations)
  • mergeConstraintsFrom(Builder $from)
  • withTrashed()
  • withoutTrashed()
  • onlyTrashed()

使用说明

Eloquent

创建模型

通常创建模型,但定义可以由输入表单数据填充的属性很重要。

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'title',
        'author',
     ];
}

创建合同

namespace App\Repositories\Contracts;

interface PostRepository
{

}

创建仓库

namespace App\Repositories;

use App\Repositories\Contracts\PostRepository as PostRepositoryContract;
use App\Post;
use Recca0120\Repository\EloquentRepository;

class PostRepository extends EloquentRepository implements PostRepositoryContract
{
    public function __construct(Post $model)
    {
        $this->model = $model;
    }
}

绑定

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repositories\Contracts\PostRepository as PostRepositoryContract;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(PostRepositoryContract::class, PostRepository::class);
    }
}

控制器

namespace App\Http\Controllers;

use App\Repositories\Contracts\PostRepository;

class PostsController extends Controller
{
    protected $repository;

    public function __construct(PostRepository $repository)
    {
        $this->repository = $repository;
    }
}

方法

在仓库中查找所有结果

$posts = $this->repository->get();

在仓库中带有分页查找所有结果

$posts = $this->repository->paginate();

在仓库中计数结果

$posts = $this->repository->count();

在仓库中创建新条目

$post = $this->repository->create(request()->all());

在仓库中更新条目

$post = $this->repository->update($id, request()->all());

在仓库中删除条目

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

新实例

$post = $this->repository->newInstance([
    'author' => 'author'
]);

根据条件返回模型

$model = $this->repository->matching(Criteria::create()->where('title', '=', 'title'));

通过ID查找结果

$post = $this->repository->find($id);

根据条件查找

使用标准

标准支持所有Eloquent函数

单个标准
use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->select('*')
    ->where('author', '=', 'author')
    ->orWhere('title', '=', 'title')
    ->orderBy('author', 'asc');

$this->repository->get($criteria);
$this->repository->paginate($criteria);

多个标准

use Recca0120\Repository\Criteria;

$criteria = [];

$criteria[] = Criteria::create()
    ->orderBy('author', 'asc');

$criteria[] = Criteria::create()
    ->where('author', '=', 'author')
    ->orWhere('title', '=', 'title');

$this->repository->get($criteria);
// $this->repository->paginate($criteria);
use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->with('author', function($criteria) {
        $criteria->where('author', 'author');
    });

$this->repository->get($criteria);
// $this->repository->paginate($criteria);

连接

use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->join('author', function ($criteria) {
        $criteria->on('posts.author_id', '=', 'author.id');
    });

$this->repository->get($criteria);
// $this->repository->paginate($criteria);

表达式

use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->where('created_at', '<=', Criteria::expr('NOW()'));

$this->repository->get($criteria);
// $this->repository->paginate($criteria);

自定义标准

use Recca0120\Repository\Criteria;

class CustomCriteria extends Criteria
{
    public function __construct($id)
    {
        $this->where('id', '=', $id);
    }
}

$this->repository->get((new CustomCriteria(1))->where('autor', 'autor'));

待办事项

  • 缓存