cuongnd88/lara-query-kit

Lara Query Kit 使用 Eloquent 模型

1.2.1 2021-07-16 10:34 UTC

This package is auto-updated.

Last update: 2024-09-20 11:14:48 UTC


README

Lara Query Kit 使用 Eloquent 模型。通过 PHP 特性和 Laravel 本地范围实现此功能

PHP 特性

PHP 只支持单一继承:子类只能从一个父类继承。

如果一个类需要继承多个行为怎么办?OOP 特性可以解决这个问题。

特性用于声明可以在多个类中使用的方法。特性可以包含可以在多个类中使用的方法和抽象方法,并且方法可以有任意的访问修饰符(公共、私有或受保护)。

Laravel 本地范围

本地范围允许您定义可以在整个应用程序中轻松重用的常见约束集。要定义范围,只需在 Eloquent 模型方法前加上 scope

安装 & 配置

1-使用 Composer 安装 cuongnd88/lara-query-kit

	composer require cuongnd88/lara-query-kit

2-将 lara-query-kit 推送到您的应用程序中。

	php artisan vendor:publish --provider="Cuongnd88\LaraQueryKit\LaraQueryKitServiceProvider"

3-App\Traits\QueryKit.php 已经准备好提高您的性能。请将 QueryKit 添加到模型中

. . . .
use App\Traits\QueryKit;

class User extends Authenticatable
{
    use Notifiable;
    use HasOtpAuth;
    use HasGuardian;
    use QueryKit;

. . . .
}

可用方法

让我们讨论 Query Kit 中可用的每个方法。

insertDuplicate

insertDuplicate(array $data, array $insertKeys, array $updateKeys): 插入新行或更新现有行。

    public function upsert()
    {
        $data = [
            ['name' => "Dean Ngo", 'email' => 'dinhcuongngo@gmail.com', 'mobile' => '84905005533', 'password' => Hash::make('123456')],
            ['name' => "Robert Neil", 'email' => '1111@gmail.com', 'mobile' => '84905001122', 'password' => Hash::make('123456')],
        ];
        User::insertDuplicate($data, ['name', 'email', 'mobile', 'password'], ['name', 'email', 'mobile']);
    }

getTableColumns

getTableColumns(): 获取列数组。

    public function listTableColumns()
    {
        $columns = User::getTableColumns();
        dump($columns);
    }

exclude

exclude(array $columns): 获取输出数据的一个子集。

您应该定义要排除哪些模型属性。您可以使用模型上的 $excludable 属性来完成此操作。

. . . .
use App\Traits\QueryKit;

class User extends Authenticatable
{
    use Notifiable;
    use HasOtpAuth;
    use HasGuardian;
    use QueryKit;

    protected $excludable = ['deleted_at', 'created_at', 'updated_at'];

. . . .
}
    public function listUsers()
    {
        $data = User::exclude()->get()->toArray();
        dump($data);
    }

或者传递一个可排除列的数组作为参数

    public function listUsers()
    {
        $users = User::exclude(['deleted_at', 'created_at', 'updated_at'])
                        ->get()->toArray();
        dump($users);
    }

filter

filter(array $params): 获取具有筛选条件的查询结果。

您可以在查询构建器实例上使用 filter 方法来向查询添加 where 子句。应该包含一个包含您要执行搜索的条件的数组 $filterable 属性。筛选数组的关键字对应于表列,而值是调用 where 子句的条件。最基本的条件需要两个参数

  • 第一个参数是一个简单的 where 子句,例如:where, orWhere, whereBetween, whereNotBetween, whereIn, whereNotIn, whereNull, whereNotNull, orWhereNull, orWhereNotNull, whereDate, whereMonth, whereDay, whereYear, whereTime

  • 第二个参数是运算符,可以是数据库支持的任何运算符。

特别地,当运算符是 LIKE 时,第三个参数是必需的,它用于指定模式

为了方便起见,如果 filterable 属性中只验证一个列,则默认子句是 where= 运算符

. . . .
use App\Traits\QueryKit;

class User extends Authenticatable
{
    use Notifiable;
    use HasOtpAuth;
    use HasGuardian;
    use QueryKit;

    protected $filterable = [
        'id' => ['whereBetween'],
        'email',
        'name' => ['orWhere', 'LIKE', '%{name}%'],
    ];

. . . .
}
    public function filter()
    {
        $where = [
            'id' => [1,5],
            'email' => 'dinhcuongngo@gmail.com',
            'name' => 'ngo',
        ];

        $data = User::->filter($where)->get()->toArray();
    }

动态地,您可以调用 filterableCondition() 并分配筛选条件

    public function filter()
    {
        $filterable = [
            'email',
            'name' => ['orWhere', 'LIKE', '%{name}%'],
            'deleted_at' => ['whereNull'],
        ];

        $where = [
            'email' => 'dinhcuongngo@gmail.com',
            'name' => 'ngo',
            'deleted_at' => '',
        ];

        $data = User::filterableCondition($filterable)
                        ->filter($where)
                        ->get()
                        ->toArray();
    }

searchFulltext

searchFulltext($value, $mode = NATURAL_LANGUAGE): 对 MySQL 表中的基于字符的数据运行全文查询。

全文搜索有四种模式

  • NATURAL_LANGUAGE(默认):在自然语言模式下
  • NATURAL_LANGUAGE_QUERY:在具有查询扩展的自然语言模式下
  • BOOLEAN_MODE:在布尔模式下
  • QUERY_EXPANSION:具有查询扩展

应该包含一个要全文搜索条件的数组 $searchable 属性

. . . .
use App\Traits\QueryKit;

class User extends Authenticatable
{
    use Notifiable;
    use HasOtpAuth;
    use HasGuardian;
    use QueryKit;


    protected $excludable = ['deleted_at', 'created_at', 'updated_at'];

    protected $searchable = [
        'name', 'address'
    ];

. . . .
}
    public function search()
    {
        $data = User::searchFulltext('ngo')->exclude()->get()->toArray();
        dump($data);
    }

您可以通过searchableCols()灵活地添加匹配的列。

    public function search()
    {
        $data = User::searchableCols(['name', 'address'])
                        ->searchFulltext('ngo')
                        ->exclude()
                        ->get()
                        ->toArray();
        dump($data);
    }

在表上运行全文查询之前,您必须先在表上创建一个全文索引。全文索引可以包括表中的一列或多列基于字符的列。

ALTER TABLE `users` ADD FULLTEXT(`name`, `address`);