The issue is with the unique index on the image_hash and is_scaled_image columns. When you delete a row, it deletes all rows with the same image_hash value, regardless of the is_scaled_image value. To fix this, you can add a where clause to the delete statement to only delete the row with the specific id value:
$tempImage = TempImage::where('image_hash', $imageHash)
->where('is_scaled_image', $isScaledImage)
->firstOrFail();
$tempImage->where('id', $tempImage->id)->delete();