{"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":1433,"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":1415,"sourceCodeEnd":1451,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Model.php#L1415-L1451","documentation":"saveOrIgnore() inserts a new row ignoring unique-constraint conflicts (INSERT IGNORE semantics) and is only valid for models not yet persisted. The guard at the top rejects calls when $this->exists is true, because an existing model would need an UPDATE path, not an INSERT-OR-IGNORE.","triggerScenarios":"Calling $existingModel->saveOrIgnore() on a model loaded from the database ($exists === true), e.g. reusing a fetched instance and attempting to insert-or-ignore it again.","commonSituations":"Treating saveOrIgnore as a generic upsert; passing a model retrieved via find()/first()/firstOrCreate() to saveOrIgnore instead of a freshly instantiated one; copy-paste from a create flow to an update flow.","solutions":["Use save() or update() for existing models instead of saveOrIgnore().","Instantiate a new model (new Model([...])) before calling saveOrIgnore() so $exists is false.","For upsert behavior on existing rows, use upsert() or firstOrCreate()/updateOrCreate() depending on intent."],"exampleFix":"// before\n$user = User::find($id);\n$user->saveOrIgnore(); // throws\n\n// after\n$user->save();\n// or for insert-or-ignore on a new row:\nUser::createOrFirst(['email' => $email], [...]);","handlingStrategy":"type-guard","validationCode":"if ($model->exists) {\n    throw new \\LogicException('Cannot saveOrIgnore on existing '.get_class($model).'; use save().');\n}\n$model->saveOrIgnore();","typeGuard":"function isInsertable(\\Illuminate\\Database\\Eloquent\\Model $model): bool {\n    return ! $model->exists;\n}","tryCatchPattern":null,"preventionTips":["Check $model->exists before saveOrIgnore; use save()/update() for existing rows.","Reserve saveOrIgnore for freshly instantiated models.","Prefer createOrFirst()/upsert() for insert-or-match semantics."],"tags":["eloquent","save","insert-ignore","persistence"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}