2016-02-12 126 views
0

我有一個雜項數據庫表,每次用戶訪問另一個用戶的配置文件時都會更新。但我在創建手動時間戳列時遇到了問題。由於我沒有爲此創建模型,因此我手動執行該模型,這不應該成爲問題,因爲此數據庫不接受任何用戶生成的輸入。Laravel手冊更新時間戳

遷移:

public function up() 
{ 
    Schema::create('recently_visited', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('visitor_id'); 
     $table->integer('profile_id'); 
     $table->integer('times_visited'); 
     $table->timestamp('last_visit'); 
    }); 
} 

控制器:

$visits = DB::table('recently_visited') 
    ->where('profile_id',$user->id) 
    ->where('visitor_id',Auth::user()->id) 
    ->increment('times_visited') 
    ->update(array('last_visit' => Carbon::now())); 

我碰到下面的錯誤。請幫我理解,這個被調用的整數是什麼。謝謝!

致命錯誤:調用一個成員函數的update()整數

答:

我只是需要包括所有我在查詢生成器的增量()內更新。

$visits = DB::table('recently_visited') 
    ->where('profile_id',$user->id) 
    ->where('visitor_id',Auth::user()->id) 
    ->increment('times_visited', 1, ['last_visit' => Carbon::now()]); 
+0

'碳::現在()'是不是一個時間戳實例。所以,試一下' - > update(array('last_visit'=> Carbon :: now() - > timestamp));'讓我知道你是否還有其他錯誤。 –

+0

仍顯示相同的錯誤。 –

+0

它與'increment()'調用。所以你應該在更新方法中使用時間戳更新。 –

回答

2

Please help me understand, what is the integer this is being called on.

increment()方法不返回查詢生成器實例,而是更新記錄本身並返回一個整數。

你應該能夠做到這一點:

$visits = DB::table('recently_visited') 
    ->where('profile_id',$user->id) 
    ->where('visitor_id',Auth::user()->id) 
    ->increment('times_visited', 1, array('last_visit' => Carbon::now())); 
+0

好的,那是有效的。我並不完全理解在查詢生成器中使用increment()的文檔,但現在就明白了。非常感激。 - > increment('times_visited',1,['last_visit'=> Carbon :: now()]); –