PHP code example of youngkingoo6 / laravel-amount

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

    

youngkingoo6 / laravel-amount example snippets


public function getAmountAttribute($value)
{
    return $value / 100;
}

public function setAmountAttribute($value)
{
    $this->attributes['amount'] = (int)($value * 100);
}

public static $amountTimes = 100;

public function getMutatedAttributes()
{
    $attributes = parent::getMutatedAttributes();
    return array_merge($attributes, $this->getAmountFields());
}

protected function mutateAttributeForArray($key, $value)
{
    return (in_array($key, $this->getAmountFields()))
        ? (function_exists('bcdiv') ? bcdiv($value, self::$amountTimes, 2) : $value / self::$amountTimes)
        : parent::mutateAttributeForArray($key, $value);
}

public function getAttributeValue($key)
{
    $value = parent::getAttributeValue($key);
    if(function_exists('bcdiv')){
        $value = bcdiv($value, self::$amountTimes,2);
    }else{
        $value = $value / self::$amountTimes;
    }

    return $value;
}

public function setAttribute($key, $value)
{
    if (in_array($key, $this->getAmountFields())) {
          if(function_exists('bcmul')){
              $value = (int)bcmul($value, self::$amountTimes);
          }else{
              $value = (int)($value * self::$amountTimes);
          }
    }
    parent::setAttribute($key, $value);
}

public function getAmountFields()
{
    return (property_exists($this, 'amountFields')) ? $this->amountFields : [];
}

  use Youngkingoo6\LaravelAmount\Traits\AmountTrait;
  

  use AmountTrait;
  

  protected $amountFields = ['amount'];
  

  use AmountTrait, BTrait {
      AmountTrait::setRawAttributes as amountTraitSetRawAttributes;
      BTrait::setRawAttributes as BTraitSetRawAttributes;
  }
  

  public function setRawAttributes(array $attributes, $sync = false)
  {
      $this->BTraitSetRawAttributes($attributes, $sync);
      $attributes = $this->getAttributes();
      $this->amountTraitSetRawAttributes($attributes, $sync);
  }