I'm getting this weird error:

SQLSTATE[42S22]:未找到列:‘field list’中有forum_threads4个未知列‘0’(sql:update forum_threads set0=LOCKED,1=1,updated_at=2016-03-17 16:01:59,其中topic_id=3,forum_threads.deleted_at为NULL)

问题是,我没有0列.我的代码中没有带0的where子句.我使用的是范围查询.

My controller is:

    $action = $request->input('action');
    $topic = $request->input('topic');
    $thread = Thread::where('topic_id', $topic);

    switch ($action) {
        case ('locked'):
            $thread->lock();
            break;
    }

正如你所见,我做的不多.我只是想锁住一根线.我正在调用我的Thread型号的锁定范围.我有很多switch 箱,其中一个是lock.我已经在顶部运行了一半的查询,所以我不必重复我自己.我只是将其存储在$thread变量中,以便执行$thread->delete()$thread->restore()之类的操作.

我在线程模型中的查询范围:

public function scopeLock($query)
{
    return $query->where('locked', 0)->update(['locked', 1]);
}

That's it. I think it may because I have a where clause passing from my controller (Thread::where('topic_id', $topic)) and I'm just continuing it onto my scope.

Any help is highly appreciated.

推荐答案

The error is due to ->update(['locked', 1]); which should be ->update(['locked' => 1]);

the update function uses an array as "column" => "value", your syntax error causes Laravel to think [ 0 => 'locked', 1 => 1], so it translates to this SQL SET 0 = 'locked', 1 = 1...

Laravel相关问答推荐

如何判断用户是否已登录Laravel

灯丝:设定模式标题

Laravel中的策略对我不起作用,这是我的代码

Laravel 数据未传递到下拉框

Ziggy 路由默认为查询字符串

验证判断请求的值是否存在于另一个表 Laravel 9 中

在 laravel 中查询此类数据的最佳做法是什么?

基于数据表 Laravel 4 导出 CSV 或 PDF

如何在 Laravel 中使用内存数据库的完整测试套件之前迁移和 seeder ?

即使上传了文件,Laravel Input::hasFile('image') 也会返回 false

Laravel:`loadMissing` 函数的目的是什么?

Laravel - 如何恢复本地存储符号链接并刷新

如何在本地 Laravel Homestead 站点上获取 https 证书

嘲笑没有匹配的关闭处理程序

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

无法声明类 Controller,因为该名称已在使用中

如何卸载 Laravel Passport

如何以及在哪里可以使用 laravel 存储图像?

如何使用 Laravel 迁移在 Mysql 列中添加注释

Laravel 4:分析器在哪里?