PHP code example of aurorawebsoftware / connective

1. Go to this page and download the library: Download aurorawebsoftware/connective library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aurorawebsoftware / connective example snippets


// config/connective.php
return [
    'connection_types' => ['friendship', 'ownership', 'parentage'],
];

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
}

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

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

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

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

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


$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)

$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

bash
php artisan vendor:publish --tag=connective-config