'datetime', 'total_price' => 'integer', 'total_weight' => 'float', ]; public function items() { return $this->hasMany(OrderItem::class); } /** * Accessor untuk mendapatkan service_name dari items * Menghindari error di view karena kolom service_name dihapus dari tabel orders */ public function getServiceNameAttribute() { if ($this->items->isEmpty()) { return 'Layanan Umum'; } return $this->items->map(function ($item) { $qty = $item->unit === 'PCS' ? round($item->qty_or_weight) . ' PCS' : number_format($item->qty_or_weight, 3, '.', '') . ' KG'; return $item->service_name . ' (' . $qty . ')||' . $item->subtotal; })->implode("\n"); } /** * Logic Booted untuk Multi-Tenancy */ protected static function booted() { static::addGlobalScope('owner', function (Builder $builder) { if (Auth::check()) { $user = Auth::user(); $ownerId = $user->getOwnerId(); $builder->where('user_id', $ownerId); } }); static::creating(function ($order) { if (Auth::check()) { $user = Auth::user(); $order->user_id = $user->getOwnerId(); } }); } }