PHP code example of mr-punyapal / laravel-extended-relationships

1. Go to this page and download the library: Download mr-punyapal/laravel-extended-relationships 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/ */

    

mr-punyapal / laravel-extended-relationships example snippets



use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;

class Post extends Model {
    use HasExtendedRelationships;

    //...
}



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



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

 

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;



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;