'date', 'quantity' => 'decimal:6', 'unit_price' => 'decimal:4', 'total_cost' => 'decimal:2', ]; } public function tracker(): BelongsTo { return $this->belongsTo(Tracker::class); } public static function totalQuantity(int $trackerId): float { return (float) static::where('tracker_id', $trackerId)->sum('quantity'); } public static function totalCost(int $trackerId): float { return (float) static::where('tracker_id', $trackerId)->sum('total_cost'); } public static function averageCostPerUnit(int $trackerId): float { $totalQuantity = static::totalQuantity($trackerId); $totalCost = static::totalCost($trackerId); return $totalQuantity > 0 ? $totalCost / $totalQuantity : 0; } }