{"id":"9ce2ccbffa0de818","repo":"laravel/framework","slug":"no-primary-key-defined-on-model","errorCode":null,"errorMessage":"No primary key defined on model.","messagePattern":"No primary key defined on model\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Model.php","lineNumber":1740,"sourceCode":"            }\n        }\n\n        return $count;\n    }\n\n    /**\n     * Delete the model from the database.\n     *\n     * @return bool|null\n     *\n     * @throws \\LogicException\n     */\n    public function delete()\n    {\n        $this->mergeAttributesFromCachedCasts();\n\n        if (is_null($this->getKeyName())) {\n            throw new LogicException('No primary key defined on model.');\n        }\n\n        // If the model doesn't exist, there is nothing to delete so we'll just return\n        // immediately and not do anything else. Otherwise, we will continue with a\n        // deletion process on the model, firing the proper events, and so forth.\n        if (! $this->exists) {\n            return;\n        }\n\n        if ($this->fireModelEvent('deleting') === false) {\n            return false;\n        }\n\n        // Here, we'll touch the owning models, verifying these timestamps get updated\n        // for the models. This will allow any caching to get broken on the parents\n        // by the timestamp. Then we will go ahead and delete the model instance.\n        $this->touchOwners();\n","sourceCodeStart":1722,"sourceCodeEnd":1758,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Model.php#L1722-L1758","documentation":"delete() checks getKeyName(); if it returns null (no primary key declared on the model), the model cannot be deleted by key and a LogicException is thrown. This usually means $primaryKey was unset/null or the model intentionally has no primary key (e.g. a pivot/view), making deletion unsupported.","triggerScenarios":"Calling $model->delete() on a model whose protected $primaryKey is set to null or empty, or on a model class where the primary key was intentionally removed (e.g. a read-only DB view mapped as a model).","commonSituations":"Mapping a database view or a join-table without a single key column; setting $primaryKey = null by mistake; using a base model class that nulls the key.","solutions":["Set a valid primary key on the model (protected $primaryKey = 'id';) matching an actual unique column.","If the model truly has no primary key, delete via a direct query (Model::where(...)->delete()) instead of instance delete().","For pivot tables, use the detach()/attach() API on the relationship rather than deleting pivot model instances."],"exampleFix":"// before\nclass UserView extends Model {\n    protected $primaryKey = null;\n}\n$view->delete(); // throws\n\n// after\nUserView::where('id', $view->id)->delete();","handlingStrategy":"type-guard","validationCode":"if (is_null($model->getKeyName())) {\n    // delete by query instead of instance\n    (get_class($model))::where(get_class($model)::query()->getQuery()->from, $model->getOriginal(...))->delete();\n}\n$model->delete();","typeGuard":"function hasPrimaryKey(\\Illuminate\\Database\\Eloquent\\Model $model): bool {\n    return ! is_null($model->getKeyName());\n}","tryCatchPattern":"try {\n    $model->delete();\n} catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'No primary key')) {\n        get_class($model)::query()->where(...)->delete();\n    }\n}","preventionTips":["Define a real $primaryKey on models that need instance delete().","Use query-based deletion for view/pivot models without a key.","Validate getKeyName() in adapters that wrap deletion."],"tags":["eloquent","delete","primary-key","schema"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}