mr-punyapal/laravel-extended-relationships

该软件包为Laravel Eloquent模型提供额外的、更有效的关系方法。

1.3 2024-03-16 14:55 UTC

This package is auto-updated.

Last update: 2024-09-02 16:02:55 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

扩展关系的需求是什么?

laravel-extended-relationships软件包为Laravel Eloquent模型提供额外的、更有效的关系方法。该软件包提供了几个有用的功能,例如减少数据库查询次数、提高性能和最小化重复代码。

我遇到了问题,然后自己创建了关系,然后意识到如果我能使用开源软件包,我也可以创建一个,于是制作了此软件包。

安装

您可以通过composer安装此软件包

composer require mr-punyapal/laravel-extended-relationships

用法

首先,在您的模型中包含HasExtendedRelationships特性

use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;

class Post extends Model {
    use HasExtendedRelationships;

    //...
}

接下来,使用belongsToManyKeys方法定义BelongsToManyKeys关系

public function auditors() {
    return $this->belongsToManyKeys(
        related: User::class,
        foreignKey: 'id',
        relations: [
            'created_by' => 'creator',
            'updated_by' => 'updater',
            'deleted_by' => 'deleter',
        ]
    );
}

此方法接受三个参数

  • 相关模型(User::class
  • 外键(id
  • 一个映射相关表的外键名称到模型上相应关系名称的数组(['created_by' => 'creator', ...]

然后,您可以像这样从审计员关系获取数据

$post = Post::with('auditors')->first();

// Get the creator
$post->auditors->creator;

// Get the updater
$post->auditors->updater;

// Get the deleter
$post->auditors->deleter;


// also works with lazy loading

$post = Post::find(7);

// Get the creator
$post->auditors->creator;

// Get the updater
$post->auditors->updater;

// Get the deleter
$post->auditors->deleter;

这允许您通过一个方法定义多个关系,并且只为所有关系在数据库中执行一个查询。

反向关系。

use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;

class User extends Model{

    use HasExtendedRelationships;

    public function audited(){
        return $this->hasManyKeys(
            related: Post::class,
            relations: [
                'created_by' => 'created', 
                'updated_by' => 'updated', 
                'deleted_by' => 'deleted',
            ],
            localKey: 'id'
        );
    }
}

要获取用户的被审计帖子,您可以使用审计关系。以下是一个示例

$user = User::with('audited')->first();

// Get posts created by the user
$user->audited->created;

// Get posts updated by the user
$user->audited->updated;

// Get posts deleted by the user
$user->audited->deleted;

// also works with lazy loading

$user = User::find(71);

// Get posts created by the user
$user->audited->created;

// Get posts updated by the user
$user->audited->updated;

// Get posts deleted by the user
$user->audited->deleted;

这允许您通过单次方法调用定义模型之间的多个关系,简化了您的代码并减少了执行的查询数。

HasManyArrayColumn

如果您在用户表中有一个名为companies的列,该列存储本地键数组[7, 71]或["7", "71"],您可以使用以下关系

use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;

class User extends Model
{
    use HasExtendedRelationships;

    protected $casts=[
       'companies' => 'array'
    ];

    public function myCompanies()
    {
        return $this->hasManyArrayColumn(
            related: Company::class,
            foreignKey: 'id',
            localKey: 'companies'
        );
    }
}

在获取数据时,您可以使用以下方式检索相关的公司

$user = User::with('myCompanies')->first();

// get companies with ids 7 and 71
$user->myCompanies;

这允许您轻松检索具有本地键数组的关联记录,这在某些场景中可能很有用。

HasManyArrayColumn的反向关系

BelongsToArrayColumn方法允许您在模型和另一个模型上的数组列之间定义关系。如果您在数组列中有一个["7", "71"],而在外键中有整数7或71,则将$isString标志设置为true以获取预期结果。

以下是一个示例

use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;

class Company extends Model
{
    use HasExtendedRelationships;

    public function companyFounders()
    {
        return $this->belongsToArrayColumn(
            related: User::class,
            foreignKey: 'id',
            localKey: 'companies',
            // optional, default is false (if true then it treats all values as string)
            isString: true 
        );
    }
}

定义了此关系后,您可以使用以下代码检索相关的公司创始人

$company = Company::with('companyFounders')->find(71);

// Founders for company with id 71

$company->companyFounders;

这将提供从users表中检索的数据,其中companies数组列包含值71。

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请查阅我们的安全策略了解如何报告安全漏洞。

致谢

许可协议

MIT许可(MIT)。请参阅许可文件以获取更多信息。