title: Traits with PHP and Laravel
body: <p>I am using Laravel 5.1 and would like to access an array on the Model from the Trait when the Model before the model uses the <code>appends</code> array. </p>
  
  <p>I would like to add certain items to the appends array if it exists from my trait. I don't want to edit the model in order to achieve this. Are traits actually usable in this scenario or should I use inheritance?</p>
  
  <pre><code>array_push($this-&gt;appends, 'saucedByCurrentUser');
  </code></pre>
  
  <p>Here is how my current setup works.</p>
  
  <p><strong>Trait</strong></p>
  
  <pre><code>&lt;?php namespace App;
  
  trait AwesomeSauceTrait {
  
    /**
     * Collection of the sauce on this record
     */
    public function awesomeSauced()
    {
      return $this-&gt;morphMany('App\AwesomeSauce', 'sauceable')-&gt;latest();
    }
    public function getSaucedByCurrentUserAttribute()
    {
      if(\Auth::guest()){
          return false;
      }
      $i = $this-&gt;awesomeSauced()-&gt;whereUserId(\Auth::user()-&gt;id)-&gt;count();
      if ($i &gt; 0){
          return true;
      }
      return false;
    }
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>&lt;?php namespace App;
  
  use App\AwesomeSauceTrait;
  use Illuminate\Database\Eloquent\Model;
  
  class FairlyBlandModel extends Model {
      use AwesomeSauceTrait;
  
      protected $appends = array('age','saucedByCurrentUser');
  
  }
  </code></pre>
  
  <p>What I would like to do is something to achieve the same effect as extending a class. I have a few similar traits, so using inheritance gets somewhat ugly.</p>
  
  <pre><code>trait AwesomeSauceTrait {
   function __construct() {
       parent::__construct();
       array_push($this-&gt;appends, 'saucedByCurrentUser');
   }
  }
  </code></pre>
  
  <p>I have <a href="https://stackoverflow.com/questions/12478124/how-to-overload-class-constructor-within-traits-in-php-5-4">seen some workarounds for this</a>, but none of them seem better/cleaner than just adding the item to the array manually. Any ideas are appreciated.</p>
  
  <p><strong>Update</strong></p>
  
  <hr/>
  
  <p>I discovered this way of accomplishing what I need for one trait, but it only works for one trait and I don't see an advantage of using this over inheritance.</p>
  
  <p><strong>trait</strong></p>
  
  <pre><code>protected $awesomeSauceAppends = ['sauced_by_current_user'];
  
  protected function getArrayableAppends()
  {
      array_merge($this-&gt;appends, $this-&gt;awesomeSauceAppends);
      parent::getArrayableAppends();
  }
  </code></pre>
  
  <p>How I am currently handling my Model, for what it is worth. </p>
  
  <p><strong>model</strong></p>
  
  <pre><code>public function __construct()
  {
      array_merge($this-&gt;appends, $this-&gt;awesomeSauceAppends);
  }
  </code></pre>