問題描述
我收到這個奇怪的錯誤:
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
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
模型中調(diào)用鎖作用域.我有很多開關(guān)盒,其中之一是lock
.我已經(jīng)在頂部運行了一半的查詢,所以我不必重復(fù)自己.我只是將它存儲在 $thread
變量中,以便我可以執(zhí)行諸如 $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 子句,而我只是將它繼續(xù)到我的范圍內(nèi).
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]);
應(yīng)該是 ->update(['locked' => 1]);
The error is due to ->update(['locked', 1]);
which should be ->update(['locked' => 1]);
更新函數(shù)使用數(shù)組作為列"=>值",你的語法錯誤導(dǎo)致 Laravel 認為 [ 0 =>'鎖定',1 =>1]
,所以它轉(zhuǎn)換成這個 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
...
這篇關(guān)于未找到列:“字段列表"中的 1054 列“0"未知 - Laravel - 我的代碼中的任何地方都沒有 0 列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!