skyraptor / laravel-encryptable-trait
Laravel 加密特性,轻松将 eloquent 模型中的某些字段设置为可加密
1.5.1
2022-06-16 11:41 UTC
Requires
- php: ^7.2|^8.0
- illuminate/encryption: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0
- phpunit/phpunit: ^7.5|^8.0|^9.0
This package is auto-updated.
Last update: 2024-09-16 16:26:59 UTC
README
这个特性会在将字段(在 $this->encryptable 中定义)保存到数据库之前对其进行加密。它通过自动加密和解密值,使得将某些字段视为可加密变得极其简单。
Photoware
此包免费使用,但受 Spaties' Poscardware 启发,我们希望能看到此包在何处被开发。您所在地区的一个重要地标的照片将非常受欢迎。
我们的电子邮件地址是 photoware@hihaho.com
安装
只需将以下行添加到您的 composer.json
文件中,然后运行 composer update
"skyraptor/laravel-encryptable-trait": "^v1.5"
或者使用以下命令通过 composer 添加
composer require skyraptor/laravel-encryptable-trait
要求
- illuminate/encryption ^6.0, ^7.0, ^8.0 或 ^9.0
- PHP 7.2, 7.3, 7.4, 8.0 或 8.1
用法
只需将特性添加到您的模型中,并将 $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