Kod berikut menunjukkan cara mengakses tetapan lalai dengan cepat:
use Illuminate\Support\Number; // 快速访问默认值 $locale = Number::defaultLocale(); $currency = Number::defaultCurrency();
<?php namespace App\Services; use App\Models\Order; use Illuminate\Support\Number; use App\Events\OrderProcessed; class OrderProcessor { public function formatOrderSummary(Order $order, ?string $userLocale = null) { $locale = $userLocale ?? Number::defaultLocale(); $currency = $order->currency ?? Number::defaultCurrency(); return [ 'order_number' => $order->reference, 'subtotal' => Number::currency($order->subtotal, in: $currency), 'tax' => Number::currency($order->tax, in: $currency), 'total' => Number::currency($order->total, in: $currency), 'formatted_date' => $order->created_at->locale($locale)->isoFormat('LLLL'), 'meta' => [ 'display_locale' => $locale, 'currency' => $currency, 'exchange_rate' => $this->getExchangeRate( from: Number::defaultCurrency(), to: $currency ) ] ]; } protected function getExchangeRate(string $from, string $to): float { // 汇率计算逻辑 return 1.0; } }
Atas ialah kandungan terperinci Mengakses lalai lokasi dan mata wang di Laravel. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!