phuongtt/genie

2.1.2 2018-07-26 07:39 UTC

This package is auto-updated.

Last update: 2024-09-27 23:33:59 UTC


README

Latest Version Software License Build Status Coverage Status Total Downloads

简介

为 Eloquent 提供一个基础仓库类,包含覆盖大多数查询的便捷方法。有助于将持久层从业务代码中抽象出来。

献给 Genie

献给世界上最好的(也是唯一的)瓶中精灵。 恭喜你获得自由,我的朋友

安装

适用于 Laravel 5.4 及以上版本

composer require phuongtt/genie ~2.0

适用于 Laravel 5.3 及以下版本

composer require phuongtt/genie ~1.0

实现

以下示例将使用一个假设的 Eloquent 模型名为 User

<?php

namespace App\Repositories;

use App\Models\User;
use Phuongtt\Genie\Repository;

class UserRepository extends Repository
{
    protected function getModel()
    {
        return new User;
    }
}

选项

Genie 已经与 Phuongtt\Bruno 集成。有关更多信息,请参阅 Bruno 文档。所有 get 方法的 $options 键采用以下格式

注意:如果您使用 Bruno 的控制器,它将自动将请求的查询字符串解析为正确格式。

API

以下示例将使用一个假设的 Eloquent 模型名为 User

get (array $options = [])

获取所有 User

getById ($id, array $options = [])

通过主键获取一个 User

getRecent (array $options = [])

获取按 created_at 降序排列的 User

getRecentWhere (string $column, mixed $value, array $options = [])

获取 $column=$valueUser 行,按 created_at 降序排列

getWhere (string $column, mixed $value, array $options = [])

获取 $column=$valueUser

getWhereArray (array $clauses, array $options = [])

通过多个 where 子句([$column1 => $value1, $column2 => $value2])获取 User

getWhereIn (string $column, array $values, array $options = [])

获取 $column 可以是 $values 中任何值的 User

getLatest (array $options = [])

获取最新的 User

getLatestWhere (string $column, mixed $value, array $options = [])

获取 $column=$value 的最新 User

delete ($id)

通过主键删除 User

deleteWhere ($column, $value)

删除 $column=$valueUser

deleteWhereArray (array $clauses)

通过多个 where 子句删除 User 行([$column1 => $value1, $column2 => $value2]

标准

此软件包符合 PSR-1PSR-2PSR-4 标准。如果您注意到符合标准的问题,请通过拉取请求发送补丁。

测试

$ phpunit

贡献

有关详细信息,请参阅 CONTRIBUTING

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件