|null $expected the version the caller read (`If-Match`); null deletes whatever the record holds now */ public function handle(Product $product, ?array $expected = null): void { DB::transaction(function () use ($product, $expected): void { // Locked until the transaction ends: the version is compared, and the // write made, against one state, so two writers holding the same // version cannot both pass. $current = Product::query()->with(['tags'])->whereKey($product->getKey())->lockForUpdate()->firstOrFail(); if ($expected !== null && ! RecordVersion::matches($current, $expected, ['tags'])) { throw new StaleRecord; } $product->setRawAttributes($current->getRawOriginal(), sync: true); $product->delete(); activity() ->performedOn($product) ->event('deleted') ->log("Deleted product \"{$product->name}\""); }); } }