HiHaHo-Interactive-Video / laravel-encryptable-trait by RobertBoes

Laravel encryptable trait, easily make certain fields for eloquent models encryptable
57,937
10
3
Package Data
Maintainer Username: RobertBoes
Maintainer Contact: robert@boes.io (Robert Boes)
Package Create Date: 2017-09-12
Package Last Update: 2024-03-25
Language: PHP
License: MIT
Last Refreshed: 2024-11-21 03:00:12
Package Statistics
Total Downloads: 57,937
Monthly Downloads: 636
Daily Downloads: 14
Total Stars: 10
Total Watchers: 3
Total Forks: 3
Total Open Issues: 0

Laravel Encryptable Trait

Build Status StyleCI Code Climate Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

This trait encrypts all your fields (defined in $this->encryptable) before saving it to the database. It makes it extremely easy to treat certain fields as encryptable by automatically encrypting and decrypting the values.

Photoware

This package is free to use, but inspired by Spaties' Poscardware we'd love to see where where this package is being developed. A photo of an important landmark in your area would be highly appreciated.

Our email address is photoware@hihaho.com

Install

Simply add the following line to your composer.json and run composer update

"hihaho/laravel-encryptable-trait": "v1.2.*"

Or use composer to add it with the following command

composer require "hihaho/laravel-encryptable-trait:v1.2.*"

Requirements

  • illuminate/encryption 5.5+ (Laravel 5.5+)
  • PHP 7.0+

Usage

Simply add the trait to your models and set the $encryptable to an array of values that need to be encrypted.

<?php

namespace app\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;
use HiHaHo\EncryptableTrait\Encryptable;

class Phone extends Eloquent
{
    use Encryptable;

    protected $encryptable = [
        'imei',
    ];
}

DecryptException

This package will thrown a DecryptException (the default Laravel one: Illuminate\Contracts\Encryption\DecryptException). You can however set $dontThrowDecryptException to true to ignore the exception. If the value can't be decrypted it will just return null.

<?php

namespace app\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;
use HiHaHo\EncryptableTrait\Encryptable;

class Phone extends Eloquent
{
    use Encryptable;

    protected $encryptable = [
        'imei',
    ];
    
    protected $dontThrowDecryptException = true;
}

If the database contains an invalid value, this will return null.

$phone = Phone::find(1);
$phone->imei; //Will return null

Contributors