PHP code example of rdx / laravel-aggregate-relationships

1. Go to this page and download the library: Download rdx/laravel-aggregate-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/ */

    

rdx / laravel-aggregate-relationships example snippets


use RelatesToAggregates;

class User extends Model {
	use RelatesToAggregates;

	// Number of transactions for this user
	function num_transactions() {
		return $this->hasCount(Transaction::class, 'user_id');
	}

	// Sum of all positive and negative transactions for this user
	function current_balance() {
		return $this->hasAggregate(Transaction::class, 'sum(money_change)', 'user_id');
	}

	// List of payment uuids
	function payment_refs() {
		return $this->hasManyScalar('reference_uuid', 'transactions', 'user_id');
	}
}

$users = User::with(['address', 'fav_color', 'num_transactions', 'current_balance', 'payment_refs'])->get();

$users = User::all();
$users->load(['address', 'fav_color', 'num_transactions', 'current_balance', 'payment_refs']);

$user = User::find(123);
echo "Balance: {$user->current_balance} ({$user->num_transactions} transactions): " .
	implode(', ', $user->payment_refs);