hihaho/laravel-encryptable-trait

Laravel 加密特性,轻松将 Eloquent 模型中的某些字段设置为可加密

v4.0.0 2024-03-25 20:08 UTC

This package is auto-updated.

Last update: 2024-09-25 21:07:08 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

此特性会在保存到数据库之前加密所有的字段(定义在 $this->encryptable 中)。通过自动加密和解密值,它使得将某些字段视为可加密变得极其容易。

Photoware

此软件包免费使用,但受 Spaties' Poscardware 的启发,我们希望能够看到此软件包的开发方向。您的地区一个重要地标的照片将会非常受欣赏。

我们的邮箱地址是 photoware@hihaho.com

安装

只需将以下行添加到您的 composer.json 中,并运行 composer update

"hihaho/laravel-encryptable-trait": "^v4.0"

或者使用以下命令通过 composer 添加

composer require hihaho/laravel-encryptable-trait

要求

  • illuminate/encryption ^10.0 或 ^11.0
  • PHP 8.1, 8.2 或 8.3

使用方法

只需将此特性添加到您的模型中,并将 $encryptable 设置为需要加密的值的数组。

<?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

此软件包将抛出 DecryptException(默认 Laravel 的:Illuminate\Contracts\Encryption\DecryptException)。但是,您可以设置 $dontThrowDecryptException 为 true 以忽略异常。如果值无法解密,则将返回 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;
}

如果数据库包含无效值,则将返回 null。

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

贡献者