title: Extending accessor and mutators in Laravel
body: <p>I'm trying to develop a small application in <code>Laravel 5.5</code> where I've made an <code>abstract model</code> which is extending <code>Laravel eloquent model</code> something like this:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Eloquent\Model;
  
  class AbstractModel extends Model
  {
  
  
  }
  </code></pre>
  
  <p>Now just for general convention I'm setting a database connection for this model:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Eloquent\Model;
  
  class AbstractModel extends Model
  {
      /**
       *  Defining connection for database
       *
       * @var string
       **/
      protected $connection='mysql';
  
  }
  </code></pre>
  
  <p>And I created my own model which extends this <code>abstract class</code>:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Eloquent\SoftDeletes;
  
  class Posts extends AbstractModel
  {
      use SoftDeletes;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'unique_id', 'title', 'contents'
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'id'
      ];
  
  }
  </code></pre>
  
  <p>By defining this by default we come to know that it will have a database connection of mysql, similarly <code>fillable</code> are defined for mass assignment. In similar way I want to make one <code>encrypt</code> variable which will <code>encrypt</code> and <code>decrypt</code> the data from the database so for this I added <code>accessor</code> and <code>mutator</code> to my <code>encrypt variable</code> inside my model:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Eloquent\SoftDeletes;
  
  class Posts extends AbstractModel
  {
      use SoftDeletes;
  
      /**
       * The attributes that will have encryption.
       *
       * @var array
       */
      protected $encryption = ['unique_id', 'title', 'contents'];
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'unique_id', 'title', 'contents'
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'id'
      ];
  
  }
  </code></pre>
  
  <p>and configured it back in my <code>abstract model</code>:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Eloquent\Model;
  
  class AbstractModel extends Model
  {
      /**
       *  Defining connection for database
       *
       * @var string
       **/
      protected $connection='mysql';
  
      /**
       *  Defining encryption
       *
       * @var array
       **/
      protected $encryption = [];
  
      public function setAttributes()
      {
          foreach ($this-&gt;encryption as $attributes)
          {
              $this-&gt;attributes[$attributes] = Crypt::encryptString($this-&gt;attributes[$attributes]);
          }
      }
  
      public function getAttributes()
      {
          foreach ($this-&gt;encryption as $attributes)
          {
              $this-&gt;attributes[$attributes] = Crypt::decryptString($this-&gt;attributes[$attributes]);
          }
      }
  }
  </code></pre>
  
  <p>I think my approach is wrong as there is no encryption or decryption done on these attributes, I can see the data sent to create rows is stored in database without any encryption and in same way retrieved also.</p>
  
  <p>More over I would like to have a use case where I'm encrypting these fields also their attribute accessor or mutator is called inside their own model for example:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Eloquent\SoftDeletes;
  
  class Posts extends AbstractModel
  {
      use SoftDeletes;
  
      /**
       * The attributes that will have encryption.
       *
       * @var array
       */
      protected $encryption = ['unique_id', 'title', 'contents'];
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'unique_id', 'title', 'contents'
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'id'
      ];
  
      public function setTitleAttributes($value)
      {
          return ucfirst($value);
      }
  
      public function getTitleAttributes($value)
      {
          return ucfirst($value)
      }
  
  }
  </code></pre>
  
  <p>Any feedback comments are welcome. Thanks.</p>