I have been looking for ideas on encrypting and decrypting values in Laravel (like VIN Numbers, Employee ID Card Numbers, Social Security Numbers, etc.) and recently found this on the Laravel website: https://laravel.com/docs/5.6/encryption
My question is, how would I print the decrypted values on a blade template? I could see going through the controller and setting a variable and then printing it to a Blade, but I was curious as to how I would also print a decrypted value to an index? Like so...
@foreach($employees as $employee)
{{$employee->decrypted value somehow}}
{{$employee->name}}
@endforeach
Create one helper file and inside that file create function that will be accessible from any views. Follow this link for create helper: https://laravelcode.com/post/how-to-create-custom-helper-in-laravel-55
and use inside view like this :
you can handle with simple create a custom trait in app/trait Encryptable.php
Use it in your models only those columns which you want to encryption.
You can handle encrypted attributes with a trait (
app/EncryptsAttributes.php
):Use it in your models when necessary:
Then you can get and set the attributes without thinking about the encryption:
You could create a custom function or an accessor in your model.
Say your model is
Employee
and your encrypted column isssn
. You could do that:In case you go with function, you'd call it like this:
And if you go with an accessor, you would call it like this:
create file
Encryptable.php
insideapp/Traits
inside your model :
use
appends
in models. easier to use anywhere without repeating using encrypt/decrypt helperuse in model