- How do I encrypt sensitive fields like passwords or API keys in Laravel using crudly/encrypted?
- Use the `Encrypted` cast on your Eloquent model attributes. For example, add `'api_key' => Encrypted::class` to your `$casts` array. The package handles encryption/decryption automatically during model serialization and retrieval.
- Does crudly/encrypted support Laravel 10 and PHP 8.2?
- Yes, the package is designed for modern Laravel versions. Check the package’s Composer requirements for exact version support, but it typically aligns with the latest LTS releases. Always verify compatibility with your project’s dependencies.
- Can I use crudly/encrypted with Laravel Scout for encrypted searchable fields?
- No, encrypted fields cannot be indexed by Scout since the data is stored in ciphertext. For searchable encrypted data, consider hybrid approaches like hashing or tokenizing sensitive fields before indexing, or use a dedicated search solution that supports encrypted queries.
- What encryption algorithm does crudly/encrypted use, and can I customize it?
- The package defaults to AES-256-GCM, a secure and performant algorithm. Customization is limited to configuration like key management; the core encryption logic is abstracted for security best practices. Review the package’s config file for available options.
- How do I handle encryption keys in production? Should I use environment variables?
- Yes, store encryption keys in environment variables (e.g., `ENCRYPTION_KEY`). The package expects a secure key during initialization. Rotate keys periodically and never hardcode them. Use Laravel’s `.env` file or a secrets manager for production deployments.
- Will encrypted fields work with Laravel’s `where` clauses or database queries?
- No, encrypted fields cannot be queried directly in `where` clauses because the data is ciphertext. Use application-level filtering or store metadata (e.g., hashed values) for searchable conditions. For complex queries, consider full-text search on non-sensitive fields.
- Is crudly/encrypted compatible with Laravel Sanctum or Passport for encrypted user data?
- Yes, but encrypt only non-critical data like addresses or preferences. Avoid encrypting authentication tokens (e.g., Sanctum/Passport payloads) as they must be accessible during token validation. Use the package for sensitive attributes while keeping auth data plaintext.
- How do I test encrypted fields in Laravel unit tests?
- Mock the encryption service or use the package’s built-in test utilities if available. Assert encrypted values by comparing decrypted outputs. For example, encrypt a test value, save it, then verify decryption matches the original. Avoid testing ciphertext directly.
- Are there performance implications for encrypting large text fields (e.g., JSON or logs)?
- Yes, encrypting large fields increases CPU and I/O overhead. For performance-critical applications, encrypt only necessary data or use compression before encryption. Benchmark with your expected payload sizes to assess trade-offs.
- What alternatives exist for encrypted Eloquent attributes in Laravel?
- Consider `laravel-encryption` for simpler use cases or `spatie/laravel-encryption` for advanced features like key rotation. For database-level encryption, use Laravel’s native `encrypt` column type or TDE (Transparent Data Encryption) in PostgreSQL/MySQL. Choose based on your need for application vs. infrastructure-level security.