PHP code example of ritechoice23 / laravel-worldable

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

    

ritechoice23 / laravel-worldable example snippets


use Ritechoice23\Worldable\Models\Country;
use Ritechoice23\Worldable\Models\Currency;

// Add foreign keys to your model
Schema::table('users', function (Blueprint $table) {
    $table->foreignId('country_id')->nullable()->constrained('world_countries');
    $table->foreignId('currency_id')->nullable()->constrained('world_currencies');
});

// Use standard relationships
class User extends Model {
    public function country() {
        return $this->belongsTo(Country::class);
    }
}

$user->country_id = Country::where('name', 'Nigeria')->first()->id;
$user->save();

use Ritechoice23\Worldable\Traits\Worldable;

class User extends Model {
    use Worldable;  // That's it!
}

// Attach world data on the fly
$user->attachCountry('Nigeria');
$user->attachCity('Lagos', 'billing');
$user->attachCurrency('NGN');
$user->formatMoney(5000);  // "₦5,000.00"

// Query naturally
User::whereFrom('Nigeria')->get();
User::wherePricedIn('USD')->get();

class Order extends Model {
    use Worldable;
}

// E-commerce checkout with multiple contexts
$order
    ->attachCountry('United States', 'billing')
    ->attachState('California', 'billing')
    ->attachCountry('Canada', 'shipping')
    ->attachCity('Toronto', 'shipping')
    ->attachCurrency('USD', 'display')
    ->attachCurrency('CAD', 'settlement');

// Analytics
$usOrders = Order::whereFrom('United States')->count();
$canadaShipping = Order::whereHas('countries', fn($q) =>
    $q->where('name', 'Canada')->wherePivot('group', 'shipping')
)->count();

// Conditional logic
if ($order->hasCountry('United States', 'billing')) {
    // Apply US tax rules
}
bash
php artisan vendor:publish --tag=worldable-config