- How do I encrypt specific fields in a Laravel Eloquent model using this package?
- Add the `Encryptable` trait to your model and define a `$encryptable` array listing the fields to encrypt. For example, `protected $encryptable = ['cc', 'ss', 'phone'];`. The package handles encryption on save and decryption on retrieval automatically.
- Does this package work with Laravel 10 or newer versions?
- Yes, the package is compatible with Laravel 8+ and newer versions. It leverages Eloquent’s standard behavior, so it integrates seamlessly with the latest Laravel releases without breaking changes.
- Can I use custom encryption algorithms (e.g., AES-256-GCM) instead of Laravel’s default?
- No, this package relies on Laravel’s built-in encryption (via `encrypt()`/`decrypt()` helpers). For custom algorithms, you’d need to extend the package or use a dedicated library like Defuse/PHP-Encryption.
- Will encrypted fields break Laravel’s query builder (e.g., `where('cc', 'LIKE', '%123%')`)?
- Yes, encrypted fields cannot be queried directly. If you need searchable encrypted data, consider using a dedicated search solution (e.g., Elasticsearch) or database-level encryption (e.g., PostgreSQL’s `pgcrypto`).
- How does this package handle key rotation if I change Laravel’s APP_KEY?
- The package uses Laravel’s default encryption key (`APP_KEY`). If you rotate the key, previously encrypted data will become unreadable. Always back up encrypted data before rotating keys, and consider field-level keys for compliance.
- Is there a performance impact when encrypting/decrypting fields on every save/load?
- Yes, encryption adds overhead. For high-write workloads, benchmark the package’s impact on your application. If performance is critical, consider database-level encryption or caching decrypted values.
- Can I encrypt only certain records (e.g., per-user keys) instead of all fields globally?
- No, this package uses a single encryption key (`APP_KEY`) for all encrypted fields. For per-record encryption, you’d need a custom solution or a package like `spatie/laravel-encryption`.
- Does this package support encrypted field indexing or partial searches?
- No, encrypted fields are stored as encrypted blobs and cannot be indexed or searched directly. For advanced search, use a dedicated encrypted search solution like OpenFHE or Elasticsearch with encrypted fields.
- How do I test models with encrypted fields in PHPUnit?
- Mock Laravel’s encryption helpers (`encrypt`, `decrypt`) in your tests to avoid relying on `APP_KEY`. Use `Hash::fake()` or `Str::random()` for predictable test data, then verify decryption works as expected.
- What are the alternatives to this package for Laravel model encryption?
- Alternatives include `spatie/laravel-encryption` (for field-level keys), `defuse/php-encryption` (custom algorithms), or database-level solutions like PostgreSQL’s `pgcrypto`. Choose based on your need for key management, compliance, or searchability.