未找到列:“字段列表”中的 1054 列“0”未知 - Laravel - 我的代码中的任何地方都没有 0 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36065951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Column not found: 1054 Unknown column '0' in 'field list' - Laravel - I don't have a 0 column anywhere in my code
提问by Taylor
I'm getting this weird error:
我收到这个奇怪的错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update
forum_threads
set0
= locked,1
= 1,updated_at
= 2016-03-17 16:01:59 wheretopic_id
= 3 andforum_threads
.deleted_at
is null)
SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“0”(SQL:更新
forum_threads
集0
= 锁定,1
= 1,updated_at
= 2016-03-17 16:01:59 其中topic_id
= 3 且forum_threads
.deleted_at
为空)
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.
问题是,我没有 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;
}
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()
.
正如你所看到的,我没有做太多事情。我只是想锁定一个线程。我正在我的Thread
模型中调用锁定范围。我有很多开关盒,其中之一是lock
. 我已经在顶部运行了一半的查询,所以我不必重复自己。我只是将它存储在$thread
变量中,以便我可以执行诸如$thread->delete()
和 之类的操作$thread->restore()
。
My query scope in Thread model:
我在线程模型中的查询范围:
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.
就是这样。我认为这可能是因为我有一个从我的控制器 ( Thread::where('topic_id', $topic)
)传递的 where 子句,我只是将它继续到我的范围内。
Any help is highly appreciated.
任何帮助都受到高度赞赏。
回答by Moak
The error is due to ->update(['locked', 1]);
which should be ->update(['locked' => 1]);
错误是由于 ->update(['locked', 1]);
哪个应该是->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 思考[ 0 => 'locked', 1 => 1]
,所以它转换为这个 SQL SET 0 = 'locked', 1 = 1
...
回答by Jilson Thomas
As I mentioned in the comment section, change your function to this:
正如我在评论部分提到的,将您的功能更改为:
public function scopeLock($query)
{
return $query->where('locked', 0)->update(['locked' => 1]);
}
Note the changes in the update method.
注意更新方法的变化。