ajangi / laravel-crypt-model
Encoding and Decoding laravel model attributes made easy.
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-10-24 16:02:22 UTC
README
Laravel-Crypt-Model
Encoding and Decoding laravel model attributes made easy.
Requirements
- minimum php version : 7.1.0
Installation
composer require ajangi/laravel-crypt-model
Hot to use?
1- Register your models
To register your models, you should pass the desired prefix and the class name of your model to.
<?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'] ] ]);
Typically, you would put the code above in a service provider.
2- Publish config file
php artisan vendor:publish --provider="LaravelCryptModel\LaravelCryptoModelServiceProvider"
then select ajangi/laravel-crypt-model to push config file. After publishing the file config/laravel-crypt-model.php
will be added.
<?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- Preparing your models
On each model that needs a hashed prefixed attribute, you should use the LaravelCryptModel\Models\Concerns\HasHashedPrefixedAttributes trait.
<?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- Getting model with prefixed hashed attributes appended to it
<?php use App\Models\User; $user = User::query() ->where('name','Alireza') ->first(); return json_encode($user);
the above code will return :
{ "id": 1, "name": "Alireza", "family": "Jangi", "mobile": "09393563537", "created_at": null, "updated_at": null, "user_id_hashed_": "user_id_hashed_7QOG8YaqVQigyD0sYEd25A==", }
5- Get model using prefixed hashed attribute
To get model using prefixed hashed attribute you can try two methods :
a. using model :
use App\Models\User; $user = User::findByPrefixedAttribute('user_id_hashed_7QOG8YaqVQigyD0sYEd25A=='); // the prefixed hashed value we get in step 4 return json_encode($user);
the above code will return :
{ "id": 1, "name": "Alireza", "family": "Jangi", "mobile": "09393563537", "created_at": null, "updated_at": null, "user_id_hashed_": "user_id_hashed_7QOG8YaqVQigyD0sYEd25A==", }
b. using 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);
the above code will return :
{ "id": 1, "name": "Alireza", "family": "Jangi", "mobile": "09393563537", "created_at": null, "updated_at": null, "user_id_hashed_": "user_id_hashed_7QOG8YaqVQigyD0sYEd25A==", }
Contributing
Take a look at CONTRIBUTING for details.