Professional Documents
Culture Documents
Laravel Eloquent - Encrypting-Decrypt Data On Call
Laravel Eloquent - Encrypting-Decrypt Data On Call
Eloquent Encrypting/Decrypt Data on call
ExampleFiles
Laravel Eloquent ‐ Encrypting/Decrypt Data on call
I can use Crypt to encrypt/decrypt my data. I want to encrypt some
information in my db (such as the name, email, phone number to name a
few).
Assuming that I want EVERYTHING to be encrypted, I want to be able to do
this in the background by itself, which I can perform by overwriting the
create and save functions:
// For instance, the save() function could become
public function save(array $options = array())
{
foreach ($this‐>attributes as $key => $value)
{
if (isset($value)) $this‐>attributes[$key] = Crypt::encrypt($value
}
return parent::save($options);
}
Now, I want the decryption to be performed the same way, so that when I
say User::find($id), the returned $user is already decrypted. Also other
functions such as firstOrFail() get() first() and all to work as well.
I also would like this functionality to be extended when I use
relationships (so User::with('someOtherTable')‐>find($id) also work).
Would this be possible? If this is not possible, I am thinking of creating
a helper function decyrpt()
function decrypt($array)
{
if (!is_array($array)) return Crypt::decrypt($array);
$result = [];
foreach($array as $key => $value) $result[$key] = decrypt($value);
return $result;
}
Laravel Eloquent Encrypting/Decrypt Data on call
And pass all my results through this first, and then start using them, but
it would be nicer if Laravel would provide this, or if there was a
"Laravel Way" of doing this.
The 7 Most Valuable Foreign Coins:
From Europe To Afric…
Answer #1:
It doesn't really make sense to encrypt everything. For example, you never
want to encrypt the primary key; that doesn't even make sense. Likewise
you probably don't want to encrypt the date fields; you'll lose the
ability to perform any sort of SQL query on them.
With that in mind, you can try something like this:
class BaseModel extends Eloquent {
protected $encrypt = [];
public function setAttribute($key, $value)
{
if (in_array($key, $this‐>encrypt))
{
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
public function getAttribute($key)
{
if (in_array($key, $this‐>encrypt))
Laravel Eloquent Encrypting/Decrypt Data on call
{
return Crypt::decrypt($this‐>attributes[$key]);
}
return parent::getAttribute($key);
}
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($attributes as $key => $value)
{
if (in_array($key, $this‐>encrypt))
{
$attributes[$key] = Crypt::decrypt($value);
}
}
return $attributes;
}
}
then have all you models extend this one, and set the $encrypt property to
whatever columns you want encrypted for that particular model.
P.S. If you want to use Eloquent's accessor functionality, you'll have to
play with this a bit more.
Answered By: Kousha
The 7 Most Valuable Foreign Coins:
From Europe To Afric…
Laravel Eloquent Encrypting/Decrypt Data on call
Answer #2:
It's worth mentioning Elocrypt library for Laravel 4. It's a more
elaborate solution that works the same way. If you're using Laravel 5 use
this one instead: Elocrypt 5 .
Answered By: Joseph Silber
The answers/resolutions are collected from stackoverflow, are licensed
under cc by‐sa 2.5 , cc by‐sa 3.0 and cc by‐sa 4.0 .
# More Articles
Using subclass type parameters in virtual functions
Array check undefined offset php
Screen capture with C# and Remote Desktop problems
Load static files for all templates in django
HTML5 Video Not Displaying on iPad
Best practices for measuring the run‐time complexity of a piece of code
Error: Property timer doesn't exist on type typeof Observable
X is not a member type of Y
How to stop a python socket.accept() call?
How does free() know how much memory to deallocate? [duplicate]
Show partitions on a pyspark RDD
How to build a pdf vignette in R and RStudio
How do /oauth/authorize and /oauth/token interact in Spring OAuth?
Log duration of an ASP Web API action
Is it possible to initialise a character array with a conditionally selected string
literal?
What's the difference between these two methods of linking a html page to a css file?
How to get Ember Data's "store" from anywhere in the application so that I can do
store.find()?
How can I use keyboard shortcuts to do a git commit and push from IntelliJ IDEA?
Laravel Eloquent Encrypting/Decrypt Data on call
Exposing a method which is inside a closure