lockForUpdate()->firstOrFail(); $mapping = DB::table('investment_item_opportunities')->where('opportunity_id', $id)->first(); if ($mapping) { return $mapping->item_id; } $item = InvestmentItem::create(['code' => 'opportunity:'.$id, 'title' => $opportunity->title, 'kind' => 'opportunity']); DB::table('investment_item_opportunities')->insert(['item_id' => $item->id, 'opportunity_id' => $id, 'mapping_evidence' => 'Direct correspondence to existing opportunity primary key; no inferred merge or change of validation status.', 'created_at' => now(), 'updated_at' => now()]); return $item->id; }); } }