Your script will continue to increment because of:
dd('added count');
.. that kills the process and so:
Session::put('id', $id);
is never run.
Try commenting out the dd() line and see if that works.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Why is my view count still increments even though I have session putted?
if(! ( Session::get($id) == $id)){
Post::where('id', $id)->increment('view_count');
dd('added count');
Session::put('id', $id);
}
Here is the problem Session::get($id). When you put in the session Session::put('id', $id); this actually means that you store some value $id by key = 'id'. To get this data back you need to use the key 'id' ... Session::get('id');
Your code trying to get some value from the Session by blog->id that stored in $id variable that some integer. Get you data back by key that you store it - 'id' (string), not blog->id (integer).
Please or to participate in this conversation.