ajangi / laravel-crypt-model
轻松编码和解码laravel模型属性。
1.0.0
2021-03-28 09:58 UTC
Requires
- php: >=7.1.0
- ext-json: *
- ext-mcrypt: *
- ext-openssl: *
- facade/ignition-contracts: ^1.0
- illuminate/contracts: ^8.0
- illuminate/database: ^8.0
This package is auto-updated.
Last update: 2024-09-24 15:45:50 UTC
README
Laravel-Crypt-Model
轻松编码和解码laravel模型属性。
要求
- 最小PHP版本:7.1.0
安装
composer require ajangi/laravel-crypt-model
如何使用?
1- 注册你的模型
为了注册模型,你需要传递想要的前缀和你的模型类名。
<?php use LaravelCryptModel\PrefixedAttributes; PrefixedAttributes::registerModels([ 'user' => [ 'model' => \App\Models\User::class, 'attributes' => ['id','avatar_file_id'] ], 'Order' => [ 'model' => \App\Models\Oredr::class, 'attributes' => ['id','customer_user_id'] ] ]);
通常,你会在服务提供者中放置上面的代码。
2- 发布配置文件
php artisan vendor:publish --provider="LaravelCryptModel\LaravelCryptoModelServiceProvider"
然后选择 ajangi/laravel-crypt-model 来推送配置文件。发布文件后,将添加 config/laravel-crypt-model.php
文件。
<?php return [ 'model_new_attribute_prefix' => 'hashed_', 'aes_secret_key' => env('AES_SECRET_KEY','6818f23eef19d38dad1d272345454549991f6368'), //the secret key you should change 'aes_secret_iv' => env('AES_SECRET_IV','73658734657823465872364587634876523487657'), //the secret iv you should change ];
3- 准备你的模型
在需要哈希前缀属性的字段上,你应该使用 LaravelCryptModel\Models\Concerns\HasHashedPrefixedAttributes 特性。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use LaravelCryptModel\Models\Concerns\HasHashedPrefixedAttributes; class User extends Model { use HasFactory,Notifiable, HasHashedPrefixedAttributes; }
4- 获取带有前缀哈希属性附加的模型
<?php use App\Models\User; $user = User::query() ->where('name','Alireza') ->first(); return json_encode($user);
上面的代码将返回
{ "id": 1, "name": "Alireza", "family": "Jangi", "mobile": "09393563537", "created_at": null, "updated_at": null, "user_id_hashed_": "user_id_hashed_7QOG8YaqVQigyD0sYEd25A==", }
5- 使用前缀哈希属性获取模型
要使用前缀哈希属性获取模型,你可以尝试两种方法
a. 使用模型
use App\Models\User; $user = User::findByPrefixedAttribute('user_id_hashed_7QOG8YaqVQigyD0sYEd25A=='); // the prefixed hashed value we get in step 4 return json_encode($user);
上面的代码将返回
{ "id": 1, "name": "Alireza", "family": "Jangi", "mobile": "09393563537", "created_at": null, "updated_at": null, "user_id_hashed_": "user_id_hashed_7QOG8YaqVQigyD0sYEd25A==", }
b. 使用 LaravelCryptModel\PrefixedAttributes
use LaravelCryptModel\PrefixedAttributes; $user = PrefixedAttributes::findModel('user_id_hashed_7QOG8YaqVQigyD0sYEd25A=='); // the prefixed hashed value we get in step 4 return json_encode($user);
上面的代码将返回
{ "id": 1, "name": "Alireza", "family": "Jangi", "mobile": "09393563537", "created_at": null, "updated_at": null, "user_id_hashed_": "user_id_hashed_7QOG8YaqVQigyD0sYEd25A==", }
贡献
查看 CONTRIBUTING 以获取详细信息。