title: Create like/unlike functionality with Laravel
body: <p>I have a list of properties for a real estate application and im trying to implement a like/unlike functionality based on each property detail. The idea is to add a like or remove it matching the current property and user. This is my code so far, but it only remove likes so it doesnt work as expected. If anyone can suggest for a better approach ill be appreciated.</p>
  
  <p>//Controller</p>
  
  <pre><code>public function storeLike($id)
  {
      $like = Like::firstOrNew(array('property_id' =&gt; $id));
      $user = Auth::id();
  
      try{
          $liked = Like::get_like_user($id);
      }catch(Exception $ex){
          $liked = null;
      }
  
      if($liked){
          $liked-&gt;total_likes -= 1;
          $liked-&gt;status = false;
          $liked-&gt;save();
      }else{
          $like-&gt;user_id = $user;
          $like-&gt;total_likes += 1;
          $like-&gt;status = true;
          $like-&gt;save();
      }
  
      return Redirect::to('/detalle/propiedad/' . $id);
  }
  </code></pre>
  
  <p>// Model</p>
  
  <pre><code>public static function get_like_user($id)
  {
      return static::with('property', 'user')-&gt;where('property_id', $id)
                                             -&gt;where('user_id', Auth::id())-&gt;first();
  }
  </code></pre>
  
  <p>// Route</p>
  
  <pre><code>Route::get('store/like/{id}', array('as' =&gt; 'store.like', 'uses' =&gt; 'LikeController@storeLike'));
  </code></pre>