aurorawebsoftware/connective

这是我打包的connective

1.0.6 2024-02-29 11:16 UTC

This package is auto-updated.

Last update: 2024-09-29 12:30:10 UTC


README

img.png

简介

Laravel Connective包提供了一种简单直观的方式来建立Eloquent模型之间的连接。它允许你定义不同的连接类型并管理模型之间的关系。

关键概念

1. 连接类型

  • 连接类型:该包支持多种连接类型,这些类型在包配置中定义。用户可以使用这些类型之一连接模型。

2. 模型

  • Connective模型:任何Eloquent模型都可以与Connective包一起使用。为了启用包的功能,模型必须实现ConnectiveContract

3. 连接

  • 连接模型:该包使用Connection模型来表示不同模型之间的关系。每个连接都有一个类型、源模型和目标模型。

安装

要开始使用Laravel Connective包,请按照以下安装步骤操作

  1. 使用Composer安装包
composer require aurorawebsoftware/connective
  1. 发布包配置文件
php artisan vendor:publish --tag=connective-config
  1. 根据您的需求更新配置文件中的连接类型。
// config/connective.php
return [
    'connection_types' => ['friendship', 'ownership', 'parentage'],
];

用法

创建Connective模型

要使用Laravel Connective包,请确保您的模型设置正确。以下是如何为包创建必要Eloquent模型的示例

确保您的模型扩展适当的Eloquent类,并在必要时实现ConnectiveContract。根据您的应用程序的要求和业务逻辑自定义模型。

以下是示例模型

use AuroraWebSoftware\Connective\Contracts\ConnectiveContract;
use AuroraWebSoftware\Connective\Models\Connection;
use AuroraWebSoftware\Connective\Traits\Connective;

class User extends Model implements ConnectiveContract
{
    use Connective

    public static function supportedConnectionTypes(): array
    {
        return ['friendship', 'parentage'];
    }

    // implementation of the model
}
use AuroraWebSoftware\Connective\Contracts\ConnectiveContract;
use AuroraWebSoftware\Connective\Models\Connection;
use AuroraWebSoftware\Connective\Traits\Connective;

class Address extends Model implements ConnectiveContract
{
    use Connective

    public static function supportedConnectionTypes(): array
    {
        return ['home', 'office'];
    }

    // implementation of the model
}

在模型之间建立连接

要建立两个模型之间的连接,可以使用connectTo方法

$sourceModel->connectTo($targetModel, 'connection_type');

将用户1连接到用户2,建立友谊(使friend1和firend2成为朋友)

连接是单向的;如果需要,必须从每个模型建立连接。

$user1->connectTo($user2, 'friendship');
$user2->connectTo($user1, 'friendship');

模型可以支持多种连接类型,并可以为同一模型类型接受多个连接。

$user1->connectTo($address1, 'home');
$user1->connectTo($address2, 'office');
$user1->connectTo($address3, 'office');

检索连接

您可以使用connections方法检索模型的连接。您可以按连接类型和目标模型类型进行过滤

$connections = $user1->connections('friendship');

// Retrieve all connections for the user
$connections = $user->connections();
// $connections is a collection of Connection models

检索连接模型

要检索源模型的连接模型(connective模型),您可以使用connectives方法。您可以按连接类型和目标模型类型进行过滤

$connectiveModels = $sourceModel->connectives('connection_type', 'target_model_type');

检索用户的朋友

$friends = $user->connectives('friend');

// 检索用户的住宅和办公室

$residences = $user->connectives(['residence', 'office'], Address::class);
// $residences is a collection of Address models (residences and offices addresses of the user)

嵌套连接

该包允许您建立嵌套连接。例如,如果模型A连接到模型B,且模型B连接到模型C,您可以通过connectives方法从模型A检索模型C。

$user = User::find(1);

// Retrieve friends of friends (nested connections)
$friendsOfFriends = $user->connectives('friend')->connectives('friend');

// $friendsOfFriends is a collection of User models (friends of friends)
$user = User::find(1);

// Retrieve office addresses of friends (nested connections)
$officesOfFriends = $user->connectives('friend')->connectives('office', Address::class);

无限嵌套

$user = User::find(1);

// Retrieve a more complex nested connection
$complexNestedConnections = $user->connectives('friend')->connectives('residence')->connectives('collaborator');

// $complexNestedConnections is a collection of models based on the specified nested connections

结论

Laravel Connective包通过提供简单且可定制的解决方案,简化了管理Eloquent模型之间关系的过程。探索包的功能,并根据您的项目需求进行定制。

这是您的Laravel Connective包文档的基本结构。请确保根据包的特定功能和需求进一步扩展和自定义它。请确保用与包相关的内容替换占位符URL和描述。