function(BeforeSheet $event) { DB::commit(); $this->backupImport->update([ 'message' => __('Importing daily changes...'), ]); DB::beginTransaction(); } ]; } public function collection(Collection $dailyChanges) { $dailyChanges->chunk($this->batchSize())->each(function ($chunk) { $this->validatePortfolioAccess($chunk); // have to cast to native values $chunk = $chunk->map(function ($dailyChange) { return [ 'total_market_value' => $dailyChange['total_market_value'], 'total_cost_basis' => $dailyChange['total_cost_basis'], 'total_gain' => $dailyChange['total_gain'], 'total_dividends_earned' => $dailyChange['total_dividends_earned'], 'realized_gains' => $dailyChange['realized_gains'], 'annotation' => $dailyChange['annotation'], 'portfolio_id' => $dailyChange['portfolio_id'], 'date' => Carbon::parse($dailyChange['date'])->format('Y-m-d') ]; }); DailyChange::upsert( $chunk->toArray(), ['portfolio_id', 'date'], [ 'total_market_value', 'total_cost_basis', 'total_gain', 'total_dividends_earned', 'realized_gains', 'annotation', 'portfolio_id', 'date' ] ); }); } public function batchSize(): int { return 500; } public function rules(): array { return [ 'portfolio_id' => ['required', 'uuid'], 'date' => ['required', 'date'], 'total_market_value' => ['sometimes', 'nullable', 'numeric'], 'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'], 'total_gain' => ['sometimes', 'nullable', 'numeric'], 'total_dividends_earned' => ['sometimes', 'nullable', 'min:0', 'numeric'], 'realized_gains' => ['sometimes', 'nullable', 'numeric'], 'annotation' => ['sometimes', 'nullable', 'string'], ]; } }