wendelladriel/laravel-more 问题修复 & 功能扩展

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

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

wendelladriel/laravel-more

最新稳定版本:v1.0.0

Composer 安装命令:

composer require wendelladriel/laravel-more

包简介

Implementation of the Repository Pattern using Laravel Model binding

README 文档

README

Implementation of the Repository Pattern using Laravel Model binding

The Repository Pattern

Repositories are used to wrap the logic to access the data sources of our applications. They can be used to improve the maintainability of an application by providing a central point in the code where the data sources are accessed.

Installation

composer require wendelladriel/laravel-more

You can publish the config file with:

php artisan vendor:publish --provider="WendellAdriel\LaravelMore\LaravelMoreServiceProvider" --tag=config

Usage

This package provides a BaseRepository class that you can extend to create your own Repositories.

Example:

<?php

namespace App\Repositories;

use App\Models\User;
use WendellAdriel\LaravelMore\BaseRepository;

class UserRepository extends BaseRepository
{
    /**
     * @param User $user
     */
    public function __construct(User $user)
    {
        parent::__construct($user);
    }
}

By creating a class like the one above you will have access to all the methods below.

Get All Records

To get all the records you can use the getAll method:

/**
 * Gets all models
 * 
 * @param array $columns
 * @return Collection
 */
public function getAll(array $columns = self::ALL_COLUMNS): Collection

Examples:

// GET ALL RECORDS WITH ALL COLUMNS
$this->userRepository->getAll();

// GET ALL RECORDS WITH SPECIFIC COLUMNS
$this->userRepository->getAll(['id', 'email']);

Get All Records By Attribute

To get all the records matching an attribute you can use the getAllBy method:

/**
 * Gets all models by the given attribute
 *
 * @param string $attribute
 * @param mixed  $value
 * @param string $compareType
 * @param bool   $withTrash
 * @return Collection
 */
public function getAllBy(string $attribute, $value, string $compareType = '=', bool $withTrash = false): Collection

Examples:

$this->userRepository->getAllBy('is_active', true);

$this->userRepository->getAllBy('type', ['admin', 'manager'], 'IN');

Get Single Record By Attribute

To get a single record matching an attribute you can use the getBy method:

/**
 * Gets a model by the given attribute
 *
 * @param string $attribute
 * @param mixed  $value
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model|null
 */
public function getBy(string $attribute, $value, string $compareType = '=', bool $withTrash = false): ?Model

Examples:

$this->userRepository->getBy('id', 1);

$this->userRepository->getBy('email', '%@gmail.com', 'LIKE');

Get Single Record By Attribute Or Fail

To get a single record matching an attribute or throw an exception if no record is found you can use the getByOrFail method:

/**
 * Gets a model by the given attribute or throws an exception
 *
 * @param string $attribute
 * @param mixed  $value
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model
 */
public function getByOrFail(string $attribute, $value, string $compareType = '=', bool $withTrash = false): Model

Examples:

$this->userRepository->getByOrFail('id', 1);

$this->userRepository->getByOrFail('email', '%@gmail.com', 'LIKE');

Get Single Record By Params

To get a single record matching multiple attributes you can use the getByParams method:

/**
 * Gets a model by some given attributes
 *
 * @param array  $params
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model|null
 */
public function getByParams(array $params, string $compareType = '=', bool $withTrash = false): ?Model

Examples:

$this->userRepository->getByParams([
    ['is_active', true],
    ['email', '%@gmail.com', 'LIKE']
])

Get Single Record By Params Or Fail

To get a single record matching multiple attributes or throw an exception if no record is found you can use the getByParamsOrFail method:

/**
 * Gets a model by some attributes or throws an exception
 *
 * @param array  $params
 * @param string $compareType
 * @param bool   $withTrash
 * @return Model
 */
public function getByParamsOrFail(array $params, string $compareType = '=', bool $withTrash = false): Model

Examples:

$this->userRepository->getByParamsOrFail([
    ['is_active', true],
    ['email', '%@gmail.com', 'LIKE']
])

Get All Records By Params

To get all the records matching multiple attributes you can use the getAllByParams method:

/**
 * Gets all models by some given attributes
 *
 * @param array  $params
 * @param string $compareType
 * @param bool   $withTrash
 * @return Collection
 */
public function getAllByParams(array $params, string $compareType = '=', bool $withTrash = false): Collection

Examples:

$this->userRepository->getAllByParams([
    ['is_active', true],
    ['email', '%@gmail.com', 'LIKE']
])

Update Records By Attribute

To update one or more records you can use the updateBy method:

/**
 * Updates one or more models
 * 
 * @param string $attribute
 * @param        $value
 * @param array  $updateFields
 * @return int
 */
public function updateBy(string $attribute, $value, array $updateFields): int

Examples:

// UPDATE SINGLE RECORD
$this->userRepository->updateBy('id', 1, ['email' => 'me@example.com']);

// UPDATE MULTIPLE RECORDS
$this->userRepository->updateBy('type', ['owner', 'manager'], ['is_active' => true]);

Delete Records By Attribute

To delete one or more records you can use the deleteBy method:

/**
 * Deletes one or more models
 * 
 * @param string $attribute
 * @param        $value
 * @return mixed
 */
public function deleteBy(string $attribute, $value)

Examples:

// DELETE SINGLE RECORD
$this->userRepository->deleteBy('id', 1);

// DELETE MULTIPLE RECORDS
$this->userRepository->deleteBy('type', ['owner', 'manager']);

Create New Record

To create a new record you can use the create method:

/**
 * Creates a new model
 *
 * @param array $args
 * @return Builder|Model
 */
public function create(array $args)

Examples:

$this->userRepository->create([
    'name' => 'John Dee',
    'email' => 'john@example.com',
    'is_active' => true,
])

Disable Global Scope

If your model has a Global Scope and you need to disable it for any queries you can use the disableGlobalScope method:

/**
 * Disables a named global scope
 *
 * @param string $scopeName
 * @return BaseRepository
 */
public function disableGlobalScope(string $scopeName): BaseRepository

Examples:

$this->userRepository->disableGlobalScope('active-users');

Enable Glogal Scope

If your model has a disabled Global Scope and you need to enable it again, you can use the enableGlobalScope method:

/**
 * Enables a named global scope
 *
 * @param string $scopeName
 * @return BaseRepository
 */
public function enableGlobalScope(string $scopeName): BaseRepository

Examples:

$this->userRepository->enableGlobalScope('active-users');

Protected Helper Methods

Besides all public methods above the BaseRepository also provides the following protected methods that you can use in your Repositories classes:

Get Table Name

If you need to get the table name for the model binded to the repository you can use the getTable method:

/**
 * Gets the table for the base model of the repository
 *
 * @return string
 */
protected function getTable(): string

Examples:

$usersTable = $this->userRepository->getTable();

New Query Util

To create new queries you can use the newQuery helper method:

/**
 * Builds a new query
 *
 * @param array|string[]|string $columns
 * @return Builder
 */
protected function newQuery(...$columns): Builder

Examples:

$this->userRepository->newQuery('id', 'email')
    ->where('is_active', true)
    ->get();

TO DO

  • Create command to generate Repositories
  • Create tests

Credits

Contributing

All PRs are welcome.

For major changes, please open an issue first describing what you want to add/change.

wendelladriel/laravel-more 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-06