2016-05-29 116 views
0

我打電話了以下功能Laravel功能不更新數據庫

function makeanote($type,$id){ 
$note = Notification::where("user_id",Auth::user()->id)->get(); 
if ($type == "user"){ 
if($id > $note->pluck("users")){ 
$note->users = $id; 
$note->save(); 
return; 
} 
} 
return; 
      } 

像這樣:makeanote($type,$id)。致電$type"user",呼叫$id"31"

在我當前用戶的數據庫中,用戶列的$note值當前爲零(0)。

因此,我認爲它會更新到31,但它保持在零。我錯誤地使用了pluck()嗎?

謝謝。

回答

1

這給一個鏡頭:

function makeanote($type,$id){ 
$note = Notification::where("user_id",Auth::user()->id)->first(); 
if ($type == "user"){ 
if($id > $note->users){ 
$note->users = $id; 
$note->save(); 
return; 
} 
} 
return; 
     } 

我的變化是:->first()(這將返回一個對象),而不是->get()(此方法返回一個對象集合),一旦你有對象,你可以訪問users直接屬性。

+0

乾杯,只是讓人們注意將來,關鍵是'first()'而不是'get()'。 –

+0

ya,對不起 - 我意識到它並不那麼清楚,所以我在答案中添加了一些解釋... – IzzEps