首页 > 解决方案 > 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

标签: mysqllaravellaravel-5eloquentlaravel-6

解决方案


你必须通知 Laravel 你正在使用与预期不同的主键:

在您的 App 模型中添加$primaryKey属性以及您在数据库中使用的主键的名称:

class App extends Model
{
    protected $primaryKey = 'appId';
}

推荐阅读