wendelladriel/laravel-more

Laravel Model绑定实现仓库模式

v1.0.0 2022-10-06 10:06 UTC

This package is auto-updated.

Last update: 2024-09-06 14:40:22 UTC


README

Laravel Model绑定实现仓库模式

仓库模式

仓库用于封装访问应用程序数据源的逻辑。它们可以通过提供一个代码中的中心点来访问数据源,从而提高应用程序的可维护性。

安装

composer require wendelladriel/laravel-more

您可以使用以下命令发布配置文件:

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

用法

此包提供了一个 BaseRepository 类,您可以扩展它来创建自己的仓库。

示例

<?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);
    }
}

通过创建一个类似于上面的类,您将能够访问以下所有方法。

获取所有记录

要获取所有记录,可以使用 getAll 方法

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

示例

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

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

按属性获取所有记录

要获取匹配属性的记录,可以使用 getAllBy 方法

/**
 * 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

示例

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

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

按属性获取单个记录

要获取匹配属性的记录,可以使用 getBy 方法

/**
 * 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

示例

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

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

按属性获取单个记录或失败

要获取匹配属性的记录或在没有找到记录时抛出异常,可以使用 getByOrFail 方法

/**
 * 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

示例

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

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

按参数获取单个记录

要获取匹配多个属性的记录,可以使用 getByParams 方法

/**
 * 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

示例

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

按参数获取单个记录或失败

要获取匹配多个属性的记录或在没有找到记录时抛出异常,可以使用 getByParamsOrFail 方法

/**
 * 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

示例

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

按参数获取所有记录

要获取匹配多个属性的记录,可以使用 getAllByParams 方法

/**
 * 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

示例

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

按属性更新记录

要更新一个或多个记录,可以使用 updateBy 方法

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

示例

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

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

按属性删除记录

要删除一个或多个记录,可以使用 deleteBy 方法

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

示例

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

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

创建新记录

要创建新记录,可以使用 create 方法

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

示例

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

禁用全局作用域

如果您的模型具有全局作用域并且您需要为任何查询禁用它,可以使用 disableGlobalScope 方法

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

示例

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

启用全局作用域

如果您的模型具有禁用的全局作用域并且您需要再次启用它,可以使用 enableGlobalScope 方法

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

示例

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

受保护的帮助方法

除了上面所有公开方法之外,BaseRepository 还提供了以下受保护方法,您可以在您的仓库类中使用这些方法

获取表名

如果您需要获取绑定到仓库的模型的表名,可以使用 getTable 方法

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

示例

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

新查询工具

要创建新查询,可以使用 newQuery 帮助方法

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

示例

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

待办事项

  • 创建生成仓库的命令
  • 创建测试

致谢

贡献

所有PR都受欢迎。

对于重大更改,请首先提交一个问题,描述您想添加/更改的内容。