PHP code example of whilesmart / eloquent-holdings

1. Go to this page and download the library: Download whilesmart/eloquent-holdings 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/ */

    

whilesmart / eloquent-holdings example snippets


use Whilesmart\Holdings\Traits\HasHoldings;

class Workspace extends Model
{
    use HasHoldings;
}

// Manual: you maintain the price
$workspace->holdings()->create([
    'name' => 'Rental flat',
    'quantity' => 1,
    'currency' => 'USD',
    'unit_price' => 120000,
]);

// Auto: refreshed from a bound provider
$workspace->holdings()->create([
    'name' => 'Bitcoin',
    'symbol' => 'BTC',
    'quantity' => 0.5,
    'currency' => 'USD',
    'price_source' => 'auto',
    'provider' => 'coingecko',
    'external_ref' => 'bitcoin',
]);

use Whilesmart\Holdings\Contracts\HoldingPriceProvider;

class CoingeckoPriceProvider implements HoldingPriceProvider
{
    public function prices(string $provider, array $externalRefs, string $currency): array
    {
        if ($provider !== 'coingecko') {
            return [];
        }
        // ...call CoinGecko, return [externalRef => price] in $currency
    }
}

// AppServiceProvider::register()
$this->app->bind(HoldingPriceProvider::class, CoingeckoPriceProvider::class);

php artisan holdings:reprice
POST /api/holdings/reprice