未找到列:“字段列表"中的 1054 列“0"未知 - Laravel - 我的代码中的任何地方都没有 0 列

2021-12-18 00:00:00 php mysql laravel laravel-5

我收到这个奇怪的错误:

I'm getting this weird error:

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列 '0'(SQL:更新 forum_threads set 0 = 锁定,1 = 1, updated_at = 2016-03-17 16:01:59 where topic_id = 3 and forum_threads.deleted_at 为空)

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update forum_threads set 0 = locked, 1 = 1, updated_at = 2016-03-17 16:01:59 where topic_id = 3 and forum_threads.deleted_at is null)

问题是,我没有 0 列.我的代码中的任何地方都没有带有 0 的 where 子句.我正在使用范围查询.

The thing is, I don't have a 0 column. I don't have a where clause with a 0 anywhere in my code. I am using a scope query.

我的控制器是:

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

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

如你所见,我做的不多.我只是想锁定一个线程.我在我的 Thread 模型中调用锁作用域.我有很多开关盒,其中之一是lock.我已经在顶部运行了一半的查询,所以我不必重复自己.我只是将它存储在 $thread 变量中,以便我可以执行诸如 $thread->delete()$thread->restore() 之类的操作.

As you can see, I don't do much. I am just trying to lock a thread. I am calling the lock scope in my Thread model. I have a lot of switch cases, one of which is lock. I have run half of the query at the top so I don't have to repeat myself. I simply stored it in the $thread variable so that I can perform actions like $thread->delete() and $thread->restore().

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

My query scope in Thread model:

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

就是这样.我认为这可能是因为我有一个从我的控制器 (Thread::where('topic_id', $topic)) 传递过来的 where 子句,而我只是将它继续到我的范围内.

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.

非常感谢任何帮助.

推荐答案

错误是由于 ->update(['locked', 1]); 应该是 ->update(['locked' => 1]);

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

更新函数使用数组作为列"=>值",你的语法错误导致 Laravel 认为 [ 0 =>'锁定',1 =>1],所以它转换成这个 SQL SET 0 = 'locked', 1 = 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...

相关文章