yadakhov/insert-on-duplicate-key
INSERT ... ON DUPLICATE KEY UPDATE (IODKU) operations in Laravel, aligning well with Eloquent’s query builder and model layer. It abstracts raw SQL complexity, making it suitable for applications relying on Eloquent models or query builder patterns.creating, updating), reducing friction in integration.InsertOnDuplicateKey) is self-contained and can be applied to Eloquent models without modifying core Laravel files. Minimal boilerplate is required for basic usage.Builder class restructuring).deleted_at timestamps if not explicitly managed.Builder or QueryException changes?UNIQUE (column1, column2))?DatabaseMigrations, RefreshDatabase)?ON DUPLICATE KEY UPDATE or Laravel’s upsert() (if available) suffice for the use case?spatie/laravel-activitylog for upserts) that offer additional features?LogEntry, UserPreference).Illuminate\Database\Query\Builder.saving lifecycle).ON DUPLICATE KEY UPDATE (all modern versions do).email, username).use Yadakhov\InsertOnDuplicateKey\InsertOnDuplicateKey;
class User extends Model {
use InsertOnDuplicateKey;
protected $duplicateKey = 'email'; // Specify unique column
}
$affected = DB::table('users')
->insertOnDuplicateKey(['name' => 'John'], ['updated_at' => now()]);
innodb_row_lock_waits in MySQL.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Trait conflicts with model methods | Silent failures or incorrect updates | Use use InsertOnDuplicateKey, Model; to avoid method clashes. |
| Composite key unsupported | Partial updates or errors | Implement custom logic or extend the trait. |
| Laravel version incompatibility | Runtime errors | Test with Laravel 9.x first; isolate changes. |
| MySQL strict mode issues | Query failures | Adjust SQL mode or handle exceptions. |
| Transaction rollback leaks | Inconsistent state | Wrap IODKU in explicit transactions. |
$duplicateKey").How can I help you explore Laravel packages today?