title: Laravel visitors counter
body: <p>I'm trying to build a visitors counter in Laravel....</p>
  
  <p>I don't know what the best place is to put the code inside so that it loads on EVERY page... But I putted it inside of the routes.php....</p>
  
  <p>I think I'll better place it inside of basecontroller?</p>
  
  <p>But okay, My code looks like this now:</p>
  
  <pre><code>    //stats
  
  $date = new \DateTime;
  
  $check_if_exists = DB::table('visitor')-&gt;where('ip', $_SERVER['REMOTE_ADDR'])-&gt;first();
  
  $get_visit_day = DB::table('visitor')-&gt;select('visit_date')-&gt;where('ip', $_SERVER['REMOTE_ADDR'])-&gt;first();
  
  $value = date_create($get_visit_day-&gt;visit_date);
  
  if(!$check_if_exists)
  {
  
      DB::table('visitor')-&gt;insert(array('ip' =&gt; $_SERVER['REMOTE_ADDR'], 'hits' =&gt; '1', 'visit_date' =&gt; $date));
  
  }else{
  
      DB::table('visitor')-&gt;where('ip', $_SERVER['REMOTE_ADDR'])-&gt;increment('hits');
  }
  
  
  $value = date_create($get_visit_day-&gt;visit_date);
  
  if ($check_if_exists &amp;&amp; date_format($value, 'd') != date('d')) {
  
      DB::table('visitor')-&gt;insert(array('ip' =&gt; $_SERVER['REMOTE_ADDR'], 'hits' =&gt; '1', 'visit_date' =&gt; $date));
  }
  </code></pre>
  
  <p>That works fine, but the problem is, my database columns always add a new value.</p>
  
  <p>So this is my database:
  <img src="https://i.stack.imgur.com/rzUa3.png" alt="With the table &#39;visitor&#39;"></p>
  
  <p>From the table 'visitor'.</p>
  
  <p>It keeps adding a new IP, hit and visit_date...</p>
  
  <p>How is it possible to just update the hits from today (the day) and if the day is passed, to set a new IP value and count in that column?</p>