Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jericopulvera's avatar

Session is not working properly

why is this not working properly? I can still refresh a lot and the view_count still increases. My controller

public function show(Request $request, $id)
    {
      
          if(!Session::get($id) == $id){
            Post::where('id', $id)->increment('view_count');
            Session::put($id, $id);
          }
           $comments = Comment::where('blog_id', $id)->with('user')->orderBy('created_at', 'ASC')->paginate(4);
           $blogs = Post::where('id', $id)->with('user')->first();
           return view('frontend.dashboard.blog-single' ,compact('blogs','comments'));
    }
  • please suggest me something if there is a better and easier way. thank you!
0 likes
3 replies
d3xt3r's avatar

Learn more about syntaxes ...

  1. !Session::get($id) == $id probably you meant (! ( Session::get($id) == $id)) or more appropriately Session::get($id) != $id

  2. Session::get($id) == $id doesn't make any sense, rather Session::get('id') == $id and Session::put('id' $id); do.

1 like
d3xt3r's avatar

Yes, missed a comma (,), bloody Monday :(

1 like

Please or to participate in this conversation.