July 2024

Laravel Encrypt Decrypt

Description

A web application built with Laravel that focuses on secure data management. The application utilizes encryption to ensure that sensitive information is securely stored within the database. It encrypts data before storing it, and decrypts it when retrieved, all handled in the model layer using Laravel's accessors and mutators.

Work Flow

Example on create new product

  1. User input data
  2. Data will pass to controller
  3. Controller will interact with related service for store data
  4. Service will store data using eloquent model (Product::create)
  5. In the model, setters are used to encrypt the data before it is stored in the database.
  6. Data will store to database in encrypted format

So, how to decrypt data?

As we know, the data is encrypted in the model before being stored in the database. Similarly, when retrieving the data, during the data retrieval with the model in the service, the model has an accessor to decrypt the columns when they are accessed.

So, the conclusion?

The encryption and decryption processes are handled in the model using accessors and mutators provided by Laravel. However, for the encryption option, the EncryptionService is used, which refers to the .env file for the encryption settings.

Example Available Data

Available Data display
Data stored in the database

Built With

Laravel 11Laravel 11