PHP code example of aichadigital / lara100

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

    

aichadigital / lara100 example snippets


0.1 + 0.2 === 0.3  // false! 😱
// Result: 0.30000000000000004

// In your database: 1999 (integer - cents)
// In your application: 19.99 (decimal - dollars/euros)

use AichaDigital\Lara100\Casts\Base100;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected function casts(): array
    {
        return [
            'price' => Base100::class,
            'cost'  => Base100::class,
            'tax'   => Base100::class,
        ];
    }
}

$product = new Product;
$product->price = 19.99;  // You set: 19.99 (decimal)
$product->save();         // DB stores: 1999 (integer cents)

echo $product->price;     // You get: 19.99 (decimal)

// Arithmetic operations work perfectly with decimals
$total = $product->price + $product->tax;  // 19.99 + 2.50 = 22.49 ✅

use AichaDigital\Lara100\Concerns\HasBase100;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasBase100;

    protected function base100Attributes(): array
    {
        return ['price', 'cost', 'tax', 'discount'];
    }
}

// Database stores: 1999 (INTEGER cents)
// Cast converts to: 19.99 (DECIMAL)
$product->price;  // 19.99

// Application receives: 19.99 (DECIMAL)
// Cast converts to: 1999 (INTEGER cents)
$product->price = 19.99;
$product->save();  // Stores 1999 in DB

use AichaDigital\Lara100\Casts\Base100;

protected function casts(): array
{
    return [
        'price' => Base100::class,                      // Uses config default
        'tax' => new Base100(PHP_ROUND_HALF_EVEN),      // Banker's rounding
        'discount' => new Base100(PHP_ROUND_HALF_DOWN), // Always round down
    ];
}

'balance' => new Base100(useBcmath: true),

$invoice = new Invoice;
$invoice->subtotal = 100.00;  // You set: $100.00 (decimal)
$invoice->tax = 13.00;        // You set: $13.00 (decimal)
$invoice->total = 113.00;     // You set: $113.00 (decimal)
$invoice->save();             // DB stores: 10000, 1300, 11300 (integers)

// Calculate percentage (works naturally with decimals)
$taxRate = ($invoice->tax / $invoice->subtotal) * 100;  // 13%

// Display to user (already a decimal!)
$formatted = '$' . number_format($invoice->total, 2);  // "$113.00"

$product = Product::find(1);  // price = 19.99 (DB has 1999)
$quantity = 3;

$lineTotal = $product->price * $quantity;  // 59.97 (19.99 × 3)
$discount = 5.00;                          // $5.00 discount
$finalTotal = $lineTotal - $discount;      // 54.97 ✅

// Works naturally with decimal arithmetic!

// Zero values
$product->price = 0.00;  // Stores 0 in DB

// Negative values (refunds, discounts)
$refund->amount = -25.00;  // Stores -2500 in DB (negative cents)

// Large numbers
$property->price = 500000.00;  // Stores 50000000 in DB ($500,000.00)

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->integer('price')->default(0);  // Stores cents: 1999 = $19.99
    $table->integer('cost')->default(0);   // Stores cents: 1500 = $15.00
    $table->integer('tax')->default(0);    // Stores cents: 250 = $2.50
    $table->timestamps();
});
bash
php artisan vendor:publish --tag="lara100-config"