2016-08-17 63 views
0

セッションで単純なビュー数を実装しました。期待通りに機能していますが、view_countの値はデータベース内で直ちに更新されますが、表示されません。私は予想される結果を得るためにページをリロードする必要があります。ここで は私のコードLaravel、ページ更新後にデータベースレコードの更新が表示される

public function viewArticle($slug){ 
    $article = Article::where('slug',$slug)->first(); 

    $articleKey = 'article_' . $article->id; 

    // Check session exists, if not then increment view count and create session key 

    if (!Session::get($articleKey)) { 
     Article::where('id', $article->id)->increment('view_count'); 
     Session::put($articleKey, 1); 
    } 

    return view('main'); 
} 

である私は、閲覧するために複数の変数を渡すが、質問に対して特異的であるようにそれらを取り除きます。

答えて

0

あなたは、それは同じまま$articleをリロードしなかったが、代わりにこれを試してください:それは働いた

public function viewArticle($slug){ 
    $article = Article::where('slug',$slug)->first(); 

    $articleKey = 'article_' . $article->id; 

    // Check session exists, if not then increment view count and create session key 

    if(!Session::get($articleKey)){ 
     $article->view_count++; 
     $article->save(); 
     Session::put($articleKey,1); 
    } 

    return view('main'); 
} 
+0

、ありがとうございました! –

関連する問題