dovutuan/laracom

基本仓库和缓存数据

1.0.2 2023-11-08 10:20 UTC

This package is auto-updated.

Last update: 2024-09-09 13:19:16 UTC


README

Laravel 仓库用于抽象数据层,使我们的应用程序更易于维护。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

安装

Composer

执行以下命令以获取软件包的最新版本

composer require dovutuan/laracom

Laravel

发布配置

php artisan vendor:publish --tag=laracom

方法

Dovutuan\Laracom\RepositoryInterface

用法

创建仓库

php artisan make:repository UserRepository

创建仓库和服务

php artisan make:repository UserRepository --ser
<?php

namespace App\Repositories;

use App\Models\User;
use Dovutuan\Laracom\DomRepository\BaseRepository;

class UserRepository extends BaseRepository
{
    public function model(): string
    {
        return User::class;
    }
}

创建服务

php artisan make:service UserService

创建服务和仓库

php artisan make:service UserService --repo
<?php

namespace App\Services;

use App\Repositories\UserRepository;

class UserService
{
    public function __construct(private readonly UserRepository $userRepository)
    {
    }
}

声明关键搜索仓库

<?php

namespace App\Repositories;

use App\Models\User;
use Dovutuan\Laracom\DomRepository\BaseRepository;

class UserRepository extends BaseRepository
{
    protected array $base_search = [
        'id' => [
            ['column' => 'id', 'operator' => OPERATOR_EQUAL]
        ],
        'keyword' => [
            ['column' => 'name', 'operator' => OPERATOR_LIKE, 'boolean' => OPERATOR_BOOLEAN_OR],
            ['column' => 'email', 'operator' => OPERATOR_LIKE, 'boolean' => OPERATOR_BOOLEAN_AND],
        ],
        'name' => [
            ['column' => 'name', 'operator' => OPERATOR_LIKE],
        ]
    ];

    public function model(): string
    {
        return User::class;
    }
}

操作符类型

const OPERATOR_EQUAL = '=';
const OPERATOR_NOT_EQUAL = '<>';
const OPERATOR_LIKE = '%%';
const OPERATOR_BEFORE_LIKE = '%_';
const OPERATOR_AFTER_LIKE = '_%';
const OPERATOR_NOT_LIKE = '!%%';
const OPERATOR_GREATER = '>';
const OPERATOR_GREATER_EQUAL = '>=';
const OPERATOR_LESS = '<';
const OPERATOR_LES_EQUAL = '<=';
const OPERATOR_IN = 'in';
const OPERATOR_NOT_IN = '!in';
const OPERATOR_NULL = 'null';
const OPERATOR_NOT_NULL = '!null';
const OPERATOR_DATE = 'date';
const OPERATOR_DATE_NOT_EQUAL = '!date';
const OPERATOR_DATE_GREATER = '>date';
const OPERATOR_DATE_GREATER_EQUAL = '>=date';
const OPERATOR_DATE_LESS = '<date';
const OPERATOR_DATE_LESS_EQUAL = '<=date';
const OPERATOR_JSON = '{}';
const OPERATOR_JSON_NOT_CONTAIN = '!{}';

布尔类型

const OPERATOR_BOOLEAN_AND = 'and';
const OPERATOR_BOOLEAN_OR = 'or';

使用方法

在仓库中通过id查找结果

$user = $this->userRepository->find(123);

在仓库中通过条件查找结果

$user = $this->userRepository->findByConditions(['id' => 123]);

在仓库中创建新条目

$user = $this->userRepository->create(Input::all());

在仓库中更新条目

$user = $this->userRepository->update(123, Input::all());

在仓库中通过条件更新条目

$user = $this->userRepository->updateByConditions(['id' => 123], Input::all());

在仓库中删除条目

$user = $this->userRepository->delete(123);

在仓库中通过条件删除条目

$user = $this->userRepository->deleteByConditions(['id' => 123]);

在仓库中按条件计数条目

$user = $this->userRepository->count(['id' => 123]);

在仓库中按条件分页条目

$user = $this->userRepository->paginate(['id' => 123]);

在仓库中按条件获取所有条目

$user = $this->userRepository->all(['id' => 123]);

在仓库中按条件插入条目

$user = $this->userRepository->inserrt([['name' => 'Hello'], ['name' => 'Hi']]);

在仓库中按条件更新或创建条目

$user = $this->userRepository->updateOrCreate(['id' => 123], ['name' => 'Hello']);

在仓库中按条件upsert条目

$user = $this->userRepository->update(['id' => 123, 'name' => 'Hello'], ['id'], ['name']);

在仓库中按条件获取所有条目并拉取

$user = $this->userRepository->allAndPluck('name', 'id', ['id' => 123]);