未找到列:1054“字段列表"中的未知列"0"-Laravel-我的代码中任何地方都没有0列 [英] Column not found: 1054 Unknown column '0' in 'field list' - Laravel - I don't have a 0 column anywhere in my code
问题描述
我遇到了这个奇怪的错误:
I'm getting this weird error:
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 为空)
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)
问题是,我没有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]);
}
就是这样.我认为这可能是因为我有一个从我的控制器传递的where子句( Thread :: where('topic_id',$ topic)
),而我只是将其继续到我的作用域中.
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.
推荐答案
该错误是由于-> 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
...
这篇关于未找到列:1054“字段列表"中的未知列"0"-Laravel-我的代码中任何地方都没有0列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!