mpyw/laravel-unique-violation-detector

此包已 废弃 并不再维护。没有建议替代包。

检测来自 PDOException 的主键/唯一键或约束违反错误。

v1.1.3 2023-08-23 04:04 UTC

This package is auto-updated.

Last update: 2023-11-21 17:06:09 UTC


README

注意

已废弃:由于 Laravel 的变化(v10.20.0),此库的功能已集成到 Laravel 核心中。 从现在开始,当存在唯一约束违反时,将抛出 Illuminate\Database\UniqueConstraintViolationException 异常,因此无需使用此库进行判断。只需使用 catchinstanceof 进行检查即可。尽管在某些部分的内部判断逻辑严格不同,但在大多数情况下应该可以替换而不存在问题。

PDOException 检测 主键/唯一键或约束违反 错误。

要求

版本
PHP ^8.0
Laravel ^9.0 || ^10.0
mpyw/unique-violation-detector ^1.0

支持的连接

数据库 连接类
MySQL Illuminate\Database\MySqlConnection
PostgreSQL Illuminate\Database\PostgresConnection
SQLite Illuminate\Database\SQLiteConnection
SQLServer Illuminate\Database\SqlServerConnection

您也可以通过以下方式添加自定义解析器:

  • Mpyw\LaravelUniqueViolationDetector\Facades\Unique::resolverFor()
  • Mpyw\LaravelUniqueViolationDetector\DetectorDiscoverer::resolverFor()

安装

composer require mpyw/laravel-unique-violation-detector

使用

您可以通过多种方式检测唯一违反。

use Mpyw\LaravelUniqueViolationDetector\UniqueViolationDetector;
use Mpyw\LaravelUniqueViolationDetector\Facades\Unique;

// Detect on a specific connection via explicitly created detector instance
// (Recommended usage in libraries)
$violated = (new UniqueViolationDetector($connection))->violated($e);

// Detect on the default connection
$violated = Unique::violated($exception);

// Detect on a specific connection via Facade
$violated = Unique::forConnection($connection)->violated($e);