mysql - Laravel 6 更新函数中的 SQLSTATE[42S22] 错误
问题描述
我正在尝试在我的 laravel 应用程序中运行更新功能,但每次运行该功能时都会出现此错误,
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `apps` set `domain` = Testinput.test.com, `apps`.`updated_at` = 2020-03-30 13:18:32 where `id` is null)
这是我在控制器中的更新功能,
public function update(Request $request,App $app, $id)
{
$this->validate($request, [
'domain' => ['required', 'string','min:2', 'max:255'],
],$request->all());
$fullDomain = $request->domain;
$app->domain=$fullDomain;
$app = App::where('appId','=','1')->first()->update($request->all());
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
我的应用表中没有名为 id 的列 我的列是 appId
解决方案
你必须通知 Laravel 你正在使用与预期不同的主键:
在您的 App 模型中添加$primaryKey
属性以及您在数据库中使用的主键的名称:
class App extends Model
{
protected $primaryKey = 'appId';
}
推荐阅读
- sorting - icetool 读取带有日期的输入文件并覆盖到另一个文件中
- mysql - sql查询正常但存储过程输出错误
- node.js - 在 nextjs HTML 中编写 nodejs 脚本
- process - nvidia-smi中的G和C类型是什么意思?
- java - 通过 javamail 应用程序读取电子邮件时出错
- java - 如何在 GraphView Android 中添加特定点?
- flutter - 如何将底栏连接到底页的顶部?
- java - Spring Data:JPA 和嵌套事务
- java - 如何将方法输出传递给数组元素?
- ruby - 如何对graphql ruby中的嵌套字段进行排序?