承接 vdhoangson/laravel-repository 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

vdhoangson/laravel-repository

Composer 安装命令:

composer require vdhoangson/laravel-repository

包简介

Repository pattern package for Laravel framework

README 文档

README

Repository pattern package for Laravel framework.

GitHub tag (latest by date) GitHub Packagist PHP from Packagist Tests

Version compatibility

Laravel

Framework Package Note
10.x.x ^2.x.x PHP ^8.2 Supported.
11.x.x ^2.x.x PHP ^8.2 Supported.
12.x.x ^2.x.x PHP ^8.2 Supported.

Requirements

  • PHP 8.3 or higher
  • Laravel 10.x or higher

Installation

You can install this package by composer:

composer require vdhoangson/laravel-repository

For more configuration, you can publish configuration file:

php artisan vendor:publish --tag=laravel-repository-config

Or using the provider:

php artisan vendor:publish --provider "Vdhoangson\LaravelRepository\Providers\LaravelRepositoryServiceProvider"

Implementation

To use Repositories, create repository class that:

  • Extend BaseRepository class
  • Implements interface that extend BaseInterface

For example, this is implementation of repository for Example entity:

ExampleRepositoryInterface:

interface ExampleRepositoryInterface extends BaseInterface
{}

ExampleRepository class. This class has to implement entity() method, that return namespace of entity that will be used by repository.

class ExampleRepository extends BaseRepository implements ExampleRepositoryInterface
{
    /**
     * Model entity class that will be use in repository
     *
     * @return BaseInterface
     */
    public function entity(): string
    {
        return Example::class;
    }

}

Using repositories

To use Repository in controller or other class you can use dependency injection or Container. Below is sample code of using service in controller.

class ExampleController extends Controller {

    /**
     * @var ExampleRepositoryInterface $exampleRepository
     */
    protected $exampleRepository;

    public function __construct(ExampleRepositorynterface $exampleRepository){
        $this->exampleRepository = $exampleRepository;
    }

    ....
}

Available methods

  • makeEntity() - make new entity instance
  • getEntity() - return previously set entity instance
  • setEntity() - set entity instance
  • pushCriteria() - push new criteria to use in query (passed class must be implementation of BaseCriteria)
  • popCriteria() - delete given criteria from use (if exist)
  • getCriteria() - return collection of actualy set criteria
  • applyCriteria() - apply criteria to use in query
  • skipCriteria() - skip criteria in query
  • clearCriteria() - clear criteria colleciton - delete all pushed criterias
  • all(array $columns) - get all records
  • get(array $columns) - get records (with criteria)
  • first(array $columns) - get first record (with criteria)
  • create(array $parameters) - create new entity record
  • updateOrCreate(array $where, array $values) - update existing record, or create new
  • update(int $id, array $parameters) - update entity by ID
  • delete(int $id) - delete entity record by ID
  • firstOrNew(array $where) - return first entity record if found, otherwise return new entity
  • orderBy(string $column, string $direction) - order records by column
  • with($relations) - add relations sub-query
  • transactionBegin() - begin database transaction
  • transactionCommit() - commit transaction
  • transactionRollback() - rollback transaction
  • findWhere(array $where, array $columns) - return all records that match where array
  • findWhereIn(string $column, array $where, array $columns)
  • findWhereNotIn(string $column, array $where, array $columns)
  • findByField($field, $value = null, $columns = ['*'])
  • chunk(int $limit, callable $callback, array $columns) - chunk query results
  • count(array $columns) - count results
  • paginate($perPage, $columns, $pageName, $page) - paginate results
  • simplePaginate($perPage, $columns, $pageName, $page) - paginate results
  • has($relation, $operator, $count, $bolean, $callback) - where has relation
  • orHas($relation, $operator, $count) - or where has relation
  • whereHas($relation, $callback, $operator, $count)
  • orWhereHas($relation, $callback, $operator, $count)
  • whereDoesntHave($relation, $callback)
  • orWhereDoesntHave($relation, $callback)
  • withCount($relations)
  • doesntHave($relation, $boolean, $callback)
  • orDoesntHave($relation)
  • hasMorph($relation, $types, $operator, $count, $boolean, $callback)
  • orHasMorph($relation, $types, $operator, $count)
  • doesntHaveMorph($relation, $types, $boolean, $callback)
  • orDoesntHaveMorph($relation, $types)
  • whereHasMorph($relation, $types, $callback, $operator, $count)
  • orWhereHasMorph($relation, $types, $callback, $operator, $count)
  • whereDoesntHaveMorph($relation, $types, $callback)
  • orWhereDoesntHaveMorph($relation, $types, $callback)
  • sum($column)

Caching

Information: In order to use Caching feature in repository, you must use cache driver that support tags. Actually "file" and "database" drivers are not supported.

More information in in laravel documentation.

You can user criteria with this functions, and results will be cached.

Repository automatically flush cache, when method create(), updateOrCreate(), update(), delete() is call.

Skipping cache

To force fetch data from database, skipping cached data, use skipCache() method. Example:

$this->repository->skipCache()->findWhere(...)
Disable cache

To quick disable cache i.ex for debugging, set REPOSITORY_CACHE variable to false in .env

REPOSITORY_CACHE=false

Use scopeQuery

$data = $this->repository->scopeQuery(function($query){
    return $query->orderBy('sort_order','asc');
})->all();

Changelog

Go to the Changelog for a full change history of the package.

License

This package is open-source software licensed under the MIT license.

vdhoangson/laravel-repository 适用场景与选型建议

vdhoangson/laravel-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 301 次下载、GitHub Stars 达 6, 最近一次更新时间为 2022 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「repository」 「laravel」 「repository pattern」 「vdhoangson」 「laravel-repository」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vdhoangson/laravel-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vdhoangson/laravel-repository 我们能提供哪些服务?
定制开发 / 二次开发

基于 vdhoangson/laravel-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 301
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-03