title: Using Laravel casts and mutators at the same time
body: <p>This is part of my Role model:</p>
  
  <pre><code>namespace App;
  
  use App\Traits\Permissions;
  use Illuminate\Database\Eloquent\Model;
  
  class Role extends Model
  {
      use Permissions;
  
      /**
       * {@inheritDoc}
       */
      protected $casts = [
          'permissions' =&gt; 'array',
      ];
  }
  </code></pre>
  
  <p>This is my Permissions trait:</p>
  
  <pre><code>namespace App\Traits;
  
  trait Permissions
  {
      public function setPermissionsAttribute($permissions)
      {
          ksort($permissions);
  
          $this-&gt;attributes['permissions'] = $permissions;
      }
      /**
       * Adds a permission.
       *
       * @param  string $permission
       * @param  bool $value
       * @return void
       */
      public function addPermission($permission, $value = true)
      {
          if (!array_key_exists($permission, $this-&gt;getAttribute('permissions'))) {
              $this-&gt;setAttribute('permissions', array_merge($this-&gt;getAttribute('permissions'), [$permission =&gt; $value]));
          }
      }
  }
  </code></pre>
  
  <p>This will throw the following exception:</p>
  
  <pre><code>QueryException in Connection.php line 769:
  Array to string conversion (SQL: update `roles` set `permissions` = 1 where `id` = 46526e77-98b8-4126-aaae-7e109c7ea227)
  </code></pre>
  
  <p>If I print the <code>$permissions</code> variable after the <code>ksort</code> operation it correctly has the permissions array:</p>
  
  <pre><code>array:1 [
    "do.this" =&gt; true
  ]
  </code></pre>
  
  <p>If I remove the <code>setPermissionsAttribute</code> function it properly serializes the array into json and stores it in the database.</p>
  
  <p>Am I doing something wrong or it's just not possible to use both these features?</p>