where('workspace_id', $workspaceId)->find($fleetNodeId) : FleetNode::query()->where('workspace_id', $workspaceId)->where('agent_id', $agentId)->first(); if (! $node) { throw new \InvalidArgumentException('Fleet node not found'); } $previousBalance = (int) CreditEntry::query() ->where('workspace_id', $workspaceId) ->where('fleet_node_id', $node->id) ->latest('id') ->value('balance_after'); return CreditEntry::create([ 'workspace_id' => $workspaceId, 'fleet_node_id' => $node->id, 'task_type' => $taskType, 'amount' => $amount, 'balance_after' => $previousBalance + $amount, 'description' => $description, ]); } }