title: Is it possible to setCookie() in Laravel 5 before returning from the controller method?
body: <p>I have some sample code within a controller method that looks up a cookie and manipulates the model if it exists, or creates a new model and returns a new cookie if it doesn't.</p>
  
  <p><strong>Is it possible to add the cookie before I return the view such that the duplicated code can be written only once?</strong></p>
  
  <p>I'm just looking for efficiency and tidiness.</p>
  
  <pre><code>$cat = Cat::find($request-&gt;cookie('cat_id'));
  if (null !== $cat) {
      if ($cat-&gt;name === 'Felix') {
          $cat-&gt;age = 10;
      } else {
          $cat-&gt;age = 8;
      }
  
      //duplicated code
      $cat-&gt;fur = 'soft';
      $cat-&gt;tail = 'wavy';
      $cat-&gt;save();
  
      return redirect('/');
  
  } else {
      $cat = new Cat;
      $cat-&gt;name = 'Ralf';
      $cat-&gt;age = 12;
  
      //duplicated code
      $cat-&gt;fur = 'soft';
      $cat-&gt;tail = 'wavy';
      $cat-&gt;save();
  
      return redirect('/')-&gt;withCookie(cookie('cat_id', $cat-&gt;id,10000));
  }
  </code></pre>