title: modify data before store with PHP Traits
body: <p>I try to Encrypt some Keys in Laravel's model with PHP trait</p>
  
  <p>this trait already encrypt value in setAttribute method but data stored without encryption in the database and I don't understand why this happens</p>
  
  <p>also when I use <code>dump</code> in setAttribute method everything is ok and values are Encrypted
      
  
  <pre><code>namespace App\EncryptorTraits;
  
  
  use blackpanda\encryptor\Encryptor;
  use Illuminate\Contracts\Encryption\DecryptException;
  use Illuminate\Contracts\Encryption\EncryptException;
  use Illuminate\Encryption\Encrypter;
  
  trait Encryptable
  {
  
      public function setAttribute($key,$val)
      {
          if($this-&gt;shouldEncrypt($key) &amp;&amp; !$this-&gt;isEncrypted($val))
          {
              $val = $this-&gt;encryptAttribute($val);
          }
  
          return parent::setAttribute($key,$val);
      }
  
      public function getAttributeFromArray($key)
      {
          return $this-&gt;doDecryptAttribute($key,parent::getAttributeFromArray($key));
      }
  
      public function getArrayableAttributes()
      {
          return $this-&gt;doDecryptAttributes(parent::getArrayableAttributes());
      }
  
      public function getAttributes()
      {
          return $this-&gt;doDecryptAttributes(parent::getAttributes());
      }
  
  
  
  
  
      public function doEncryptAttributes($key)
      {
          if($this-&gt;shouldEncrypt($key) &amp;&amp; !$this-&gt;isEncrypted($this-&gt;attributes[$key])){
              $this-&gt;attributes[$key] = $this-&gt;encryptAttribute($this-&gt;attributes[$key]);
          }
      }
  
      public function encryptAttribute($value)
      {
  
          try {
              $encrypted = $this-&gt;getEncrypter()-&gt;encrypt($value);
          } catch (EncryptException $e) {
              throw new EncryptException($e-&gt;getMessage(), $e-&gt;getCode());
          }
  
          return $this-&gt;getEncryptionPrefix() . $encrypted;
      }
  
  
      public function decryptAttribute($value)
      {
          if( !$this-&gt;isEncrypted($value) ) return $value;
  
          try{
              $decrypted = $this-&gt;getEncrypter()-&gt;decrypt(str_replace($this-&gt;getEncryptionPrefix(), '', $value));
          }
          catch (DecryptException $e)
          {
              throw new DecryptException($e-&gt;getMessage(),$e-&gt;getCode());
          }
  
          return $decrypted;
      }
  
      public function doDecryptAttribute($key , $val)
      {
          if($this-&gt;shouldEncrypt($key) &amp;&amp; $this-&gt;isEncrypted($val))
          {
              return $this-&gt;decryptAttribute($val);
          }
  
          return $val;
      }
  
      public function doDecryptAttributes($attributes)
      {
          foreach ($attributes as $key =&gt; $val)
          {
              $attributes[$key] = $this-&gt;doDecryptAttribute($key,$val);
          }
  
          return $attributes;
      }
  
  
  
  
  
      protected function getEncryptionPrefix()
      {
          return config('encryptor.db_encryption_prefix');
      }
  
      protected function getEncryptableList()
      {
          return (isset($this-&gt;encryptable)) ? $this-&gt;encryptable : [];
      }
  
      protected function shouldEncrypt($key) : bool
      {
          $encryptableList = $this-&gt;getEncryptableList();
  
          return (in_array($key, $encryptableList));
      }
  
      protected function isEncrypted($value)
      {
          return strpos((string)$value, $this-&gt;getEncryptionPrefix()) === 0;
      }
  
      protected function getEncrypter()
      {
          return new Encrypter($this-&gt;getEncryptionSecret(),'AES-256-CBC');
      }
  
      protected function getEncryptionSecret()
      {
          $encryptor = new Encryptor();
          return $encryptor-&gt;getDatabaseSecret();
      }
  }
  </code></pre>
  
  <p>I try to store data Like This </p>
  
  <pre><code>$Create = \App\myModel::create([
          'key1' =&gt; 'val1',
          'key2' =&gt; 'val2',
          ..
          ..
   ]);
  </code></pre>
  
  <p>and also I tried Save method but data still stored unencrypted in the database!</p>
  
  <p>and here is my model</p>
  
  <pre><code>&lt;?php
  
  
  namespace App;
  
  
  use App\EncryptorTraits\Encryptable;
  use Illuminate\Database\Eloquent\Model;
  
  class myModel extends Model
  {
      use Encryptable;
  
      protected $encryptable = ['name'];
  
      protected $guarded = ['id'];
      protected $table = 'table';
  
  }
  </code></pre>