đMastering Encryption in Laravel: A Complete Guide with Real-World Examples
A Real-World Guide to Securely Storing Sensitive Data (Like Client Bank Info) in Laravel
In todayâs digital world, data is one of the most valuable assets â and also one of the most vulnerable. Whether youâre building a SaaS platform, a healthcare portal, or a financial app, protecting sensitive information is not optional. Laravel makes this easier with its built-in, developer-friendly encryption system.
This guide will help you understand:
What encryption is
How Laravel handles encryption
When you should use it
Best practices every developer must follow
Real-world use cases to apply in your projects
Letâs dive in! đ§
đ§Š 1. What Is Encryption?
Encryption is the process of converting readable data (plaintext) into unreadable data (ciphertext) using a key.
Only someone with the correct key can decrypt the data.
Why We Need Encryption?
To protect sensitive data
To comply with security standards
To secure database-stored information
To prevent identity theft, fraud, or data leaks
Laravelâs Encryption
Laravel uses OpenSSL and the industry-standard AES-256-CBC or AES-128-CBC encryption.
It is:
Secure
Fast
Easy to use
Opinionated (removes complexity)
âď¸ 2. How Laravel Encryption Works
Laravel uses a secret key stored in .env â APP_KEY.
APP_KEY=base64:JQ1XC77tVRa1Tt56Zl................This key is used to encrypt/decrypt values.
âď¸ Encrypting Data
use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString(âHello Priteshâ);âď¸ Decrypting Data
use Illuminate\Support\Facades\Crypt;
$decrypted = Crypt::decryptString($encrypted);đ 3. When Should You Use Encryption?
You should NOT encrypt everything.
Use encryption when storing or transmitting highly sensitive information such as:
âď¸ Personally identifiable information (PII)
Aadhaar / SSN
Phone numbers
Government IDs
Passport numbers
âď¸ Financial data
Bank account details
Credit card numbers
Transaction tokens
âď¸ Authentication-related data
API keys
Access tokens
Refresh tokens
OAuth secrets
âď¸ Business confidential data
Internal notes
Private documents
Signed agreements
â When NOT to Use Encryption
Fields used for search or filtering (
LIKE,WHERE)Sorting fields
Non-sensitive fields
Because encrypted data is unreadable, database-level queries become impossible.
â 4. Best Practices for Using Encryption in Laravel
â
1. Always protect your APP_KEY
Never share your .env file
Never commit it to GitHub
Rotate keys during breaches
Use server-level secrets management (AWS/GCP/Vault)
â
2. Use model encryption casts
Cleaner, automatically applied, and standardized.
protected $casts = [
âpan_numberâ => âencryptedâ,
];
â
3. Encrypt only what is necessary
Avoid performance overhead and indexing problems.
â
4. Store hash values for searchable encrypted fields
Example: store hashed email for lookup.
email_encrypted
email_hash (for searching)â
5. Use Queue Encryption for Queued Jobs
Never put raw sensitive data in queues.
$job->data = Crypt::encrypt($sensitive);â
6. Secure backups
Encrypted data must remain encrypted even in backups.
â
7. Donât log sensitive information
Disable logging for encrypted sensitive data.
đŚ Real-World Scenario: Storing Client Bank Details (EFT)
Imagine youâre building an insurance or loan management system where clients can set up their bank account for EFT payouts.
You want to store:
Account holder name
Bank name
Account number (Sensitive đ¨)
Routing number (Sensitive đ¨)
Bank identifiers (IFSC / SWIFT / Branch code)
This must be safe.
Letâs build it the secure way.
đ§Š Step 1: Create the Migration
Hereâs the migration youâll use:
Schema::create(âclient_bank_detailsâ, function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger(âclient_idâ);
$table->string(âaccount_holder_nameâ);
$table->string(âbank_nameâ);
// Sensitive fields - stored encrypted
$table->text(âaccount_numberâ);
$table->text(ârouting_numberâ);
$table->enum(âaccount_typeâ, [âcheckingâ, âsavingsâ, âbusinessâ]);
$table->boolean(âis_primaryâ)->default(false);
$table->boolean(âis_verifiedâ)->default(false);
$table->enum(âstatusâ, [âactiveâ, âinactiveâ])->default(âactiveâ);
$table->string(âbranch_codeâ)->nullable();
$table->string(âswift_codeâ)->nullable();
$table->string(âcountryâ)->nullable();
$table->string(âcurrencyâ)->nullable();
$table->json(âmetadataâ)->nullable();
$table->timestamps();
});đĄď¸ Step 2: Apply Laravel Encryption Using Model Casts
This is the secret sauce.
Laravel provides a beautiful, painless way to automatically encrypt/decrypt model attributes.
Just add:
protected $casts = [
âaccount_numberâ => âencryptedâ,
ârouting_numberâ => âencryptedâ,
];Your model becomes:
class ClientBankDetail extends Model
{
protected $fillable = [
âclient_idâ,
âaccount_holder_nameâ,
âbank_nameâ,
âaccount_numberâ,
ârouting_numberâ,
âaccount_typeâ,
âis_primaryâ,
âis_verifiedâ,
âstatusâ,
âbranch_codeâ,
âswift_codeâ,
âcountryâ,
âcurrencyâ,
âmetadataâ,
];
protected $casts = [
âaccount_numberâ => âencryptedâ,
ârouting_numberâ => âencryptedâ,
âmetadataâ => âarrayâ,
âis_primaryâ => âbooleanâ,
âis_verifiedâ => âbooleanâ,
];
}đ Step 3: Storing Encrypted Bank Information
ClientBankDetail::create([
âclient_idâ => 101,
âaccount_holder_nameâ => âJohn Doeâ,
âbank_nameâ => âWells Fargoâ,
âaccount_numberâ => â9876543210â,
ârouting_numberâ => â021000021â,
âaccount_typeâ => âcheckingâ,
âcountryâ => âUSAâ,
âcurrencyâ => âUSDâ
]);Laravel will:
Encrypt the data before storing it
Decrypt it when you retrieve it
Protect it using application key (
APP_KEY)
All automatically.
đ Step 4: Reading Decrypted Values
$bank = ClientBankDetail::find(1);
echo $bank->account_number;You get a clean value like:
9876543210
But if you open the database, you see unreadable encrypted content â exactly what we want.
đ Compliance & Legal Benefits
Encryption helps meet:
PCI-DSS
GDPR
SOC 2
Encryption â compliance
But compliance is impossible without encryption.
đ Conclusion
Laravel makes encryption incredibly simple without compromising security.
If youâre dealing with sensitive data, encryption is non-negotiable.
This blog covered:
âď¸ What encryption is
âď¸ How Laravel handles it
âď¸ When to use it
âď¸ Best practices
âď¸ Real-world examples
âď¸ Practical code snippets


