{"record":{"id":"653682c12c7d3c5b","repo":"laravel/framework","slug":"cannot-use-saveorignore-on-an-existing-model","errorCode":null,"errorMessage":"Cannot use saveOrIgnore on an existing model.","messagePattern":"Cannot use saveOrIgnore on an existing model\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Model.php","lineNumber":1435,"sourceCode":"        // we need to happen after a model gets successfully saved right here.\n        if ($saved) {\n            $this->finishSave($options);\n        }\n\n        return $saved;\n    }\n\n    /**\n     * Save the model to the database, ignoring specific unique constraint conflicts.\n     *\n     * @param  array  $options\n     * @param  array|string|null  $uniqueBy\n     * @return bool\n     */\n    public function saveOrIgnore(array $options = [], array|string|null $uniqueBy = null)\n    {\n        if ($this->exists) {\n            throw new LogicException('Cannot use saveOrIgnore on an existing model.');\n        }\n\n        $this->mergeAttributesFromCachedCasts();\n\n        $query = $this->newModelQuery();\n\n        if ($this->fireModelEvent('saving') === false) {\n            return false;\n        }\n\n        $saved = $this->performInsertOrIgnore($query, $uniqueBy);\n\n        if (! $this->getConnectionName() &&\n            $connection = $query->getConnection()) {\n            $this->setConnection($connection->getName());\n        }\n\n        if ($saved) {","sourceCodeStart":1417,"sourceCodeEnd":1453,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Model.php#L1417-L1453","documentation":"saveOrIgnore() is designed only for INSERTs of new models using INSERT ... ON CONFLICT DO NOTHING (or equivalent). When called on a model whose $exists flag is true (already loaded from the DB or previously saved), it throws because the operation is undefined for existing rows - there is no insert to ignore a unique conflict on. Use save()/update() for existing models.","triggerScenarios":"Calling $model->saveOrIgnore() after $model was fetched with find()/first()/get(), or after a prior save() that set $exists=true. Also after replicating a model without unsetting exists.","commonSituations":"Re-using a hydrated model instance to do an 'insert if not exists' idempotently; a bulk import loop that re-saves existing rows; replicating then saveOrIgnore without first marking the copy as new.","solutions":["For inserts of new records, use a fresh model: $m = new Model; $m->fill(...); $m->saveOrIgnore();.","For existing records, call save() or update() instead of saveOrIgnore().","If you replicated a model, the copy already has exists=false; otherwise unset it, but prefer new instances.","For upsert behavior on existing rows use upsert() / insertOrIgnore() at the query level."],"exampleFix":"// before\n$user = User::find($id);\n$user->saveOrIgnore(); // throws: $exists is true\n\n// after\n$user->save(); // update the existing row\n// or for a fresh insert\n(new User(['name' => $x]))->saveOrIgnore();","handlingStrategy":"type-guard","validationCode":"if ($model->exists) {\n    throw new \\LogicException('Use save()/update() for existing models; saveOrIgnore is insert-only.');\n}\n$model->saveOrIgnore();","typeGuard":"function isNewModel(\\Illuminate\\Database\\Eloquent\\Model $m): bool {\n    return ! $m->exists;\n}","tryCatchPattern":null,"preventionTips":["Treat saveOrIgnore as insert-only - never call it on a model loaded from the DB.","Use new Model([...]) (or Model::make([...])) for idempotent inserts.","For upsert semantics on possibly-existing rows, use upsert() or insertOrIgnore() at the query level.","After replicate(), the copy is already new - safe to saveOrIgnore."],"tags":["eloquent","persistence","insert","save-or-ignore","upsert"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}