<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mathiasgrimm / laravel-encrypted-attributes example snippets
echo $tenant->access_token_decrypted;
logger($tenant, ['tenant' => $tenant]);
// Will output:
// [2022-06-23 12:10:32] local.DEBUG: {"access_token_github":"plain-text-string"} {"tenant":{"App\\Models\\Tenant":{"access_token_github":"plain-text-string"}}}
print_r($tenant->toArray());
// Will print:
// Array
// (
// [access_token_github] => plain-text-string
// )
echo $tenant->toJson();
// Will print: {"access_token_github":"plain-text-string"}
echo $tenant->access_token_github;
// Will print: plain-text-string
// app/Models/Tenants.php
use MathiasGrimm\EncryptedAttributes\HasEncryptedAttributes;
class Tenants extends Model
{
use HasEncryptedAttributes;
protected $encrypted = [
'access_token_github',
'access_token_facebook',
];
}
// other.php
$tenant = Tenant::find(1);
// -------------------------------------------------------------------
// Example 1 - New Default Getter Behaviour
// -------------------------------------------------------------------
echo $tenant->access_token_github;
// Will print "local.encrypted-string".
// When using the accessor we always get the raw database value.
// Therefore, by default it's encrypted.
// -------------------------------------------------------------------
// Example 2 - (Same) Default Setter Behaviour
$tenant->access_token_github = 'plain-text-string';
// Will encrypt the value to "local.encrypted-string".
// -------------------------------------------------------------------
// Example 3 - Getting The Decrypted Value
// -------------------------------------------------------------------
echo $tenant->access_token_github_decrypted;
// Will print "plain-text-string".
// -------------------------------------------------------------------
// Example 4 - Setting a Raw Value
// -------------------------------------------------------------------
$tenant->access_token_github_raw = 'local.encrypted-string';
// Will store the raw value
echo $tenant->access_token_github;
// Will print "local.encrypted-string".
echo $tenant->access_token_github_decrypted;
// Will print "plain-text-string".
// -------------------------------------------------------------------
// Example 5 - Update
// -------------------------------------------------------------------
$tenant->update([
'access_token_github' => 'plain-text-string'
]);
// Will store/set the value "local.encrypted-string"
// -------------------------------------------------------------------
// Example 5.1 - Update With Raw
// -------------------------------------------------------------------
$tenant->update([
'access_token_github_raw' => 'local.encrypted-string'
]);
// Will store/set the value of access_token_github to "local.encrypted-string"
// -------------------------------------------------------------------
// Example 6 - Query
// -------------------------------------------------------------------
echo Tenant::where('access_token_github', 'plain-text-string')->count();
// Will print "0"
echo Tenant::where('access_token_github', 'local.encrypted-string')->count();
// Will print "1"