Laravel datatables with encrypted data

The Laravel datatables package is brilliant by Arjay Angeles. It makes making datatables with server-side processing a cinch! To make it work, have a look at the docs.

One thing I had a little bit of a problem with is what if you have encrypted data stored in the database? Simply use a collection:

$usersData = [];
$users = \App\User::all();
foreach($users as $user) {
        $usersData[] = [
            'id'            => $user->id,
            'email'         => $user->email,
            'created_at'    => $user->created_at,
            'updated_at'    => $user->updated_at,
            'title'         => $user->title,
            'firstname'     => $user->firstname,
            'lastname'      => $user->lastname,
            'level'         => $user->level,
            'confirmed_email' => $user->confirmed_email
        ];
    }

    $collection = new Collection($usersData);

    return Datatables::of($collection)->make(true);

Here is my User model and Support model, which I forgot to post.

User.php

class User extends Support implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
{
  use Authenticatable, Authorizable, CanResetPassword;

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';

  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = ['email', 'password', 'firstname', 'lastname', 'confirmation_code', 'title', 'level'];

  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  protected $hidden = ['password', 'remember_token'];

  protected $encrypted = ['firstname', 'lastname'];
}

Support.php

class Support extends Model
{
  protected $encrypted = [];

  public function getAttribute($key)
  {
      if (array_key_exists($key, array_flip($this->encrypted))) {
          return Crypt::decrypt(parent::getAttribute($key));
      }

      return parent::getAttribute($key);
  }

  public function setAttribute($key, $value)
  {
      if (array_key_exists($key, array_flip($this->encrypted))) {
          parent::setAttribute($key, Crypt::encrypt($value));
          return;
      }

      parent::setAttribute($key, $value);
  }
}