Installation
composer require oleander29/decrypt:dev-master
Register the service provider and facade in config/app.php:
'providers' => [
// ...
Oleander29\Decrypt\DecryptServiceProvider::class,
],
'aliases' => [
// ...
'Decrypt' => 'Oleander29\Decrypt\DecryptServiceFacade',
],
Define Encryptable Fields
In your Eloquent model (e.g., User.php), declare fields to decrypt:
protected $encryptable = [
'password',
'api_token',
];
First Use Case Decrypt a single model or collection in a controller:
use Decrypt;
public function show(User $user)
{
$decrypted = Decrypt::model($user);
return response()->json($decrypted);
}
API Responses
Use Decrypt::model() or Decrypt::collection() to sanitize sensitive data before JSON serialization:
return response()->json(Decrypt::collection(User::all()));
Resource Transformers
Integrate with Laravel's Illuminate\Http\Resources\Json\JsonResource:
public function toArray($request)
{
return Decrypt::model($this->resource);
}
Middleware for Decryption Automatically decrypt models in API routes:
public function handle($request, Closure $next)
{
$request->merge(['decrypted' => Decrypt::model($request->user)]);
return $next($request);
}
Dynamic Field Handling
Override $encryptable per model instance (e.g., for partial decryption):
$user->setEncryptable(['email']); // Temporarily restrict decryption
Scout::index($model)->toSearchableArray(Decrypt::model($model));
$this->app->instance('Decrypt', Mockery::mock('Decrypt'));
Facade Dependency
Avoid direct instantiation of DecryptManager; always use the facade (Decrypt::model()).
Fix: Refactor to inject the facade or service container binding.
Collection vs. Model Behavior
Decrypt::collection() returns an array, not a Collection instance. Chain with collect() if further processing is needed:
collect(Decrypt::collection(User::all()))->filter(...);
Circular References
Decrypting nested relationships (e.g., user->posts) may cause infinite loops if models reference each other.
Fix: Explicitly define $encryptable for related models or use ->with() to limit depth.
Performance Decrypting large collections in loops can be slow. Cache decrypted data:
$cacheKey = 'decrypted_users_' . $user->id;
return Cache::remember($cacheKey, now()->addHours(1), fn() => Decrypt::model($user));
Field Not Decrypted?
Verify $encryptable is defined in the model and the field name matches exactly (case-sensitive).
Tip: Use dd($model->getEncryptable()) to inspect dynamic overrides.
Facade Not Found?
Ensure the service provider and alias are registered in config/app.php after Laravel's core providers.
Custom Decryption Logic Bind a custom decrypter to the container:
$this->app->bind('decrypt', function () {
return new CustomDecrypter();
});
Conditional Decryption
Extend the DecryptManager to add rules (e.g., decrypt only if request()->has('decrypt_all')):
public function model($model, bool $force = false)
{
if (!$force && !$this->shouldDecrypt()) {
return $model->toArray();
}
// ... existing logic
}
Non-Eloquent Models
Decrypt arrays manually by passing a custom array to the underlying decryptFields() method (check the service provider's bindings).
How can I help you explore Laravel packages today?