Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Decrypt Laravel Package

oleander29/decrypt

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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',
    ],
    
  2. Define Encryptable Fields In your Eloquent model (e.g., User.php), declare fields to decrypt:

    protected $encryptable = [
        'password',
        'api_token',
    ];
    
  3. 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);
    }
    

Implementation Patterns

Core Workflows

  1. API Responses Use Decrypt::model() or Decrypt::collection() to sanitize sensitive data before JSON serialization:

    return response()->json(Decrypt::collection(User::all()));
    
  2. Resource Transformers Integrate with Laravel's Illuminate\Http\Resources\Json\JsonResource:

    public function toArray($request)
    {
        return Decrypt::model($this->resource);
    }
    
  3. 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);
    }
    
  4. Dynamic Field Handling Override $encryptable per model instance (e.g., for partial decryption):

    $user->setEncryptable(['email']); // Temporarily restrict decryption
    

Integration Tips

  • Laravel Scout: Decrypt fields before indexing:
    Scout::index($model)->toSearchableArray(Decrypt::model($model));
    
  • Laravel Nova: Customize detail views by decrypting fields in tool methods.
  • Testing: Mock the facade for unit tests:
    $this->app->instance('Decrypt', Mockery::mock('Decrypt'));
    

Gotchas and Tips

Pitfalls

  1. Facade Dependency Avoid direct instantiation of DecryptManager; always use the facade (Decrypt::model()). Fix: Refactor to inject the facade or service container binding.

  2. 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(...);
    
  3. 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.

  4. 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));
    

Debugging

  • 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.

Extension Points

  1. Custom Decryption Logic Bind a custom decrypter to the container:

    $this->app->bind('decrypt', function () {
        return new CustomDecrypter();
    });
    
  2. 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
    }
    
  3. Non-Eloquent Models Decrypt arrays manually by passing a custom array to the underlying decryptFields() method (check the service provider's bindings).

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope