php - Laravel 自定义主键不适用于数据更新
问题描述
我正在尝试使用自定义主 ID 在 laravel 中创建一个 crud 函数。创建、读取和删除都在工作,但每当我尝试更新时,都会出现错误:
SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:从
pages
whereslug
= about 和id
<> 3 中选择 count(*) 作为聚合)
模型 pages.php
class Page extends Model{
use HasFactory;
protected $primaryKey = 'pages_id';
protected $fillable = [
'is_default_home',
'is_default_not_found',
'title',
'slug',
'content',
];}
控制器 pages.php
public function update()
{
$this->validate();
$this->unassignedDefaultHomePage();
$this->unassignedDefaultNotFoundPage();
Page::find($this->modelId)->update($this->modelData());
$this->modalFormVisible = false;
$this->reset();
}
模型数据
public function modelData()
{
return [
'title' => $this->title,
'slug' => $this->slug,
'content' => $this->content,
'is_default_home' => $this->isSetToDefaultHomePage,
'is_default_not_found' => $this->isSetToDefaultNotFoundPage,
];
}
我不确定它为什么会找到“id”列,因为我将模型中的 $primaryKey 设置为“pages_id”。我正在使用 phpMyAdmin (xampp)。感谢您的帮助!
解决方案
这是来自count
查询的错误,而不是来自update
查询的错误。我认为您需要检查其他功能。更新功能没问题。