lara-pack/repository 问题修复 & 功能扩展

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

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

lara-pack/repository

最新稳定版本:v1.0.0

Composer 安装命令:

composer require lara-pack/repository

包简介

Laravel Repository Concept

README 文档

README

A minimal structured repository pattern concept package for Laravel, providing a flexible MasterDataRepository class to handle common CRUD actions, complex where clauses, and dynamic queries for your models.

Installation

You can install the package via composer:

composer require lara-pack/repository

(Note: Ensure your package repository is configured locally or the package is published on Packagist)

Usage

Create a new repository class and extend LaraPack\Repository\MasterDataRepository.

Implement the className() method which should return the eloquent Model class name this repository handles.

1. Creating a Repository

namespace App\Repositories;

use LaraPack\Repository\MasterDataRepository;
use App\Models\User;

class UserRepository extends MasterDataRepository
{
    protected static function className(): string
    {
        return User::class;
    }
}

2. Available Methods

Here are the various methods you can use out of the box with MasterDataRepository.

all()

Retrieves all records.

$users = UserRepository::all();

find($id)

Finds a specific record by its primary key.

$user = UserRepository::find(1);

create($data)

Creates a new record.

$user = UserRepository::create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => bcrypt('password')
]);

update($id, $data)

Updates a record by its primary key.

$status = UserRepository::update(1, ['name' => 'Jane Doe']);

delete($id)

Deletes a record (or soft deletes if the model uses the SoftDeletes trait).

$status = UserRepository::delete(1);

forceDelete($id)

Permanently deletes a record bypassing soft deletes.

$status = UserRepository::forceDelete(1);

3. Dynamic Filtering (getBy, findBy, countBy, updateBy, deleteBy, forceDeleteBy)

The true power of this repository package is its whereClause processing. You can dynamically pass arrays of conditions.

Structure of Where Clause

Each clause is an array structure that follows this format:

  • Basic condition: ['column', 'value'] (operator defaults to =)
  • With specified operator: ['column', 'operator', 'value']
  • With IN operator: ['column', ['value1', 'value2']]
  • With OR conjunction: ['column', 'operator', 'value', 'OR']

Retrieve a single record (findBy)

// Find the first user where name = 'John'
$user = UserRepository::findBy([
    ['name', '=', 'John']
]);

// You can request a "lock for update" on the row by passing true as the 2nd argument:
$lockedUser = UserRepository::findBy([['id', 1]], true);

Retrieve multiple records (getBy)

// Getting users where status is active AND role IN (admin, manager)
// Order by created_at DESC
$users = UserRepository::getBy(
    [
        ['status', 'active'], // omitted operator defaults to '='
        ['role', ['admin', 'manager']], // using an array as value defaults to 'IN'
    ],
    [
        ['created_at', 'DESC'] // Order by clause
    ]
);

// Advanced conditions (Using OR)
$users = UserRepository::getBy([
    ['role', 'admin'],
    ['status', '=', 'banned', 'OR'] // Will map to OR WHERE status = 'banned'
]);

Count records (count(), countBy())

$totalUsers = UserRepository::count();

$activeUsersCount = UserRepository::countBy([
    ['status', 'active']
]);

Update or Delete based on conditions

// Update all users where status = 'pending'
UserRepository::updateBy([['status', 'pending']], ['status' => 'active']);

// Delete all users where status = 'banned'
UserRepository::deleteBy([['status', 'banned']]);

// Force delete users based on condition
UserRepository::forceDeleteBy([['status', 'banned']]);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固