定制 laravelutilities/repository 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

laravelutilities/repository

最新稳定版本:v3.1.0

Composer 安装命令:

composer require laravelutilities/repository

包简介

Add-on on Laravel Models with caching implementation

README 文档

README

This package allows you to create Model Repository with ease. You can use this package as an add-on on existing laravel model classes. Hightlights are given below

  • Cache Repository
  • Model Repository
  • Make Repository Artisan Commands

Getting Started

1. Install

Run the following command:

composer require laravelutilities/repository

2. Publish

Publish config file.

php artisan vendor:publish --tag=repository

4. Configure

You can change the options of your app from config/repository.php file

Usage

You can inject the dependency of a repository in any controller and use its functions like $this->repository->fetchByCreatedAt('foo') or if you want to use cache $this->repository->fetchByCreatedAt('foo', 'cache')

Variations

$this->repository->findByFieldFromCache('created_at', 'somevalue'); // extending cache repository; fetch only single value
$this->repository->findByFieldsFromCache(['created_at' => 'somevalue', 'city' => 'someanothervalue',...]); // fetch multiple values
$this->repository->findByField('created_at', 'somevalue'); // extending model repository; fetch only single value
$this->repository->findByField('created_at', 'somevalue'); // extending model repository; fetch only single value

public function fetchByFieldFromCache($key, $value);
public function fetchByFieldsFromCache(array $fieldsAndValues);

public function getById($id);
public function getByIds(array $ids)

Controller Constructor

use App\Repositories\OrganizationRepository;

protected $organization;
    
public function __construct()
{
    $this->organization = new OrganizationRepository();
}

Repository Class with Database

namespace App\Repositories;

use App\Models\AppLog;
use LaravelUtility\Repository\Repositories\ModelRepository;

class AppLogRepository extends ModelRepository
{
    public function __construct()
    {
        parent::__construct(new AppLog());
    }
}

Repository Class with Cache

namespace App\Repositories;

use App\Models\AppLog;
use LaravelUtility\Repository\Repositories\CacheRepository;

class AppLogRepository extends CacheRepository
{
    public function __construct()
    {
        parent::__construct(new AppLog());
    }
}

Model Repository Methods

public function findByField($key, $value);
public function findByFields(array $fieldsAndValues);

public function fetchByField($key, $value);
public function fetchByFields(array $fieldsAndValues);

Cache Repository Methods

public function findByFieldFromCache($key, $value);
public function findByFieldsFromCache(array $fieldsAndValues);

public function fetchByFieldFromCache($key, $value);
public function fetchByFieldsFromCache(array $fieldsAndValues);

public function getById($id);
public function getByIds(array $ids)

Accessor Trait

A Trait has been added to further augment these functions using magic method. In all the above mentioned functions, Field can be replaced in any of the model field. Suppose, you have a field created_at inside your table; this can be called in variaous ways given below

$this->repository->findByCreatedAt('somevalue'); // extending model repository; fetch only single value

$this->repository->findByCreatedAt('somevalue','cache'); // extending cache repository; fetch only single value

$this->repository->fetchByCreatedAt(['created_at' => 'somevalue', 'city' => 'someanothervalue',...]); // fetch multiple values

Make Repository Command

php artisan make:repository <name> --model=<model> --cache=true/false

You can create Repository classes using make:repository command.

<name> is mandatory; specifies the name of the repository. Using Repository as a suffix is mandatory. And if you don't write the name with Repository as suffix, command do for you.

--model=modelname as option if you don't menthion the name of the model, it tries to find the model same as Repository .if model is not created it ask if you want to creat new model.

cache=true|false, default is true, your repository extends CacheRepository else Model Repository

Changelog

Please see Releases for more information what has changed recently.

License

The MIT License (MIT). Please see LICENSE for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固